Jump to content

SgtBot

Member
  • Posts

    77
  • Joined

  • Last visited

Everything posted by SgtBot

  1. Hi, I have a pi hole working as the DNS server for my network, forcing all traffic to use it as the main DNS server. But I also want to set up an openvpn client profile on my router. I already have an openvpn server set up on one of my vps's. If I do this will the traffic coming through my network still have ads blocked? Or will I need to configure my vps with the vpn on it to use a custom DNS server? Thanks! Edit: Router is a newer ASUS router
  2. Thanks, I'll look into that. I think I've got some pretty good baseline code for it. For the pushbullet one, I use that as well. I have my google nest hub send a pushbullet command to my phone, which uses Automagic on my phone to send a WOL packet to my pc. I used Automagic because Taskr didn't have native WOL support. Here's the code I have so far, I'll look into implementing that push2run into the code if I can: import os import time from wakeonlan import send_magic_packet phone = "1.1.1.1" #Phone IP address computer = "2.2.2.2" #Computer IP address while True: time.sleep(20) #Ping for computer and/or phone every 20 seconds phone_response = os.system("ping -c 1 " + phone) # Initial ping for phone if phone_response == 0: print("Owner phone detected on network, sending magic packet...") # Sends WOL packet if phone is detected send_magic_packet('aa:bb:cc:dd:ee:ff') else: pc_response = os.system("ping -c 1 " + computer) if pc_response != 0: # This block checks if waking up the computer was waked print("Owner computer still offline, resending magic packet...") # and continues to ping until successful send_magic_packet('aa:bb:cc:dd:ee:ff') else: print("Owner phone and computer online, standing by...") # If owner and computer are both online, don't send any packets pass else: print("Owner phone offline, retrying...") # If phone is offline, start script over pass
  3. Thanks! I made a basic python script to implement that. I assigned my phone's mac address a static IP through DHCP on my router so I could still have the phone utilize pi-hole. Then I have the script ping that IP until it gets a successful ping, upon which it will send a magic packet to wake up the computer. The only issue is that it will keep repeatedly sending magic packets while my phone is on the network, so I've just got to edit it to send once on a successful ping to phone. I'll probably end up setting a variable to count how many wake on lan packets have been sent, and then increment it on each one. Then to make sure it actually wakes up the computer, I'll have the script ping my computer's IP and if the computer is still off it will reset the WOL packet counter to zero and try again. Idk if thats the most effective but it seems like a good shot so far Here's what I've got so far if you're curious: import os import time from wakeonlan import send_magic_packet hostname = "1.1.1.1" #My phone while True: time.sleep(20) response = os.system("ping -c 1 " + hostname) if response == 0: print("Owner phone detected on network, sending magic packet...") send_magic_packet('aa:bb:cc:dd:ee:ff') #Sends WOL packet to computer else: print("Owner phone offline, retrying...") pass
  4. Thanks! I already have WOL enabled but i just use an app from my phone to send the packet. I just need to have it automate itself on certain conditions from a rpi
  5. Hi, I'm currently trying to set up some quality of life smart home applications with a raspberry pi 4 I recently got. Right now all it has on it is pi-hole to filter out all of the ads on my network. My goal is to be able to have the raspberry pi be able to send a wake on lan packet to my computer when I either tell my google home to turn on my computer and/or when it detects that my phone's mac address has reconnected to my home network. I would also like to be able to have the reverse happen, I want to send a command to my computer (running windows 10 home) to shut down on either of the above conditions (except obviously when my phone's mac address leaves the home network). Here are my basic quesions: What is the most effective way to send a wake on lan packet from a raspberry pi to another device on the same network? Is there a Python library that would allow me to send WOL packets? Would I have to enable remote registry on my windows machine or is it possible to write a custom windows application that listens for instructions from the pi and executes a batch file to shut down? I can probably figure out how to listen for ifttt requests on the raspi and have the pi execute a command or script in response... but any suggestions on how to do this efficiently would be greatly appreciated! Any other smart home ideas like this with the pi would be welcomed as well. I'm currently working on modifying the pi to act as a smart thermostat as well to work with an AWS server so I can control my apartments temperature from anywhere. Thanks!
  6. I'm trying to make a basic navigation bar for a website, and I am adding social media icons in the navigation bar as well. I have some basic styling done with it to get the social media icons to actually sit in the navigation bar, but I feel like the way I do it will break on different devices. Below is a screenshot of what the navbar looks like and the css code that is organizing it. Any advice or tips would be appreciated :) body { background-image: url("keto.jpg"); } .navbar { font-family: "Times New Roman", Times, serif; font-style: normal; font-size: 20px; } .navleft { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } .navleft li { float: left; } .navleft a { display: block; color: pink; text-align: center; padding: 14px 16px; text-decoration: none; } .navleft a:hover { background-color: #FFC0CB; color: #333; font-style: oblique; } .navright { list-style-type: none; margin-top: -42px; padding: 0; overflow: hidden; background-color: #333; } .navright li { float: right; display: inline; } .navright a { padding: 5px 5px; } .navright img { width: 35px; height: 35px; }
  7. I switched to shutil.move for the command, so the new code looks like: for i in range(len(fileList)): if fileList[i].split('.')[1] == fileType: if i == 0: try: os.mkdir(fileType) except FileExistsError: print("File already exists, continuing...") pass shutil.move(fileList[i], fileType) count = count+1 else: break But when I try to run the program in my folder, it doesn't even recognize that there are any word documents. I tried to run it in an empty folder with that one word document and it worked... but it seems when in needs to sort through multiple files it has problems.
  8. Hi, I am working on a file sorter in python to sort different file types into their respective folders so I can go through them later. Here is my code so far: import os loop = 'y' while loop == 'y': count = 0 fileType = input("What type of file would you like to sort? ") fileList = [] for files in os.walk("."): for filename in files[2]: fileList.append(filename) for i in range(len(fileList)): if fileList[i].split('.')[1] == fileType: if i == 0: try: os.mkdir(fileType) except FileExistsError: print("File already exists, continuing...") pass os.system("move "+fileList[i]+" "+fileType) count = count+1 else: break if count != 0: print("Moved", count, "file(s) into folder:", fileType) else: print("No files matching that type.") loop = input("\nSort more files? (y/n): ") It works great, but the only problem that I have is that it seems to bug out whenever the file name itself has a space in it. For example, when I tried to make it move a .docx document with the title: AS121 - HW10 - REGULATORY GUIDANCE, CERTIFICATES AND DOCUMENTS-2.docx, it said it moved the file, but did nothing instead. Here is a screenshot of cmd while it's running: I'm not sure why it would be doing this... I checked the fileList and split lines in my code, and it seems to split that word document perfectly fine into the name and the docx. Here is a screenshot of that: I'm not sure why it could be doing this. Do I have to have some kind of error handling to tell the program what to do when there is a space in the file name? I feel like it shouldn't matter since I don't split the file by spaces I split it by a period. Any help would be greatly appreciated!
  9. That code definitely works well, so another question I ask is if I wanted to read the entire contents of a file and print them out, say in this case if it had numbers and letters. Would that be possible with this code or would I need to instead read the binary of the file and convert it after I read the file?
  10. That is actually pretty embarrassing for me... I guess programming at 4 am isn't always the best idea. Anyways, I fixed those issues in my code now, and my new problem is a singular error stating that I am using too many arguments to the fopen function as shown below:
  11. Hi, I am trying to write a program that will print out the contents of a file. I also wanted to try making the program take an argument in the command line. Here is my code: #include <stdio.h> int readFile(char *infile) { int num; FILE *fptr; fptr = open("/home/students/hansong5/crypter/%c", infile, "r"); if(fptr == NULL) { printf("Error opening file"); } fscanf(fptr, "%d", &num); fclose(fptr); return num; } int main (int argc, char *argv[]) { printf(readFile(argv[1])); return 0; } I am having a number of problems. The first being that this code does not compile without warnings or errors, as shown below: For some reason, when I try to read the file using fopen, I get the error: "too many arguments". I am trying to specify the path name by putting the %c at the end of the path and specifying that it should use the first argument in the command as the path name. Then, I get a warning about making a pointer from an integer when using printf. Should I define a variable outside of the printf statement with the function instead of passing the function inside of the printf statement? The second problem is that when I try to run the code with warnings I get a segmentation fault. I believe that is just because I am misusing the pointers in my code but I'm not sure how to go about fixing this. Any help would be greatly appreciated! Disclaimer: While in my filepath you can see "student" as one of the directories, this particular project is not a homework assignment. I program mostly on a redhat server that the university provides us. I am trying to learn about reading and writing files and writing C programs with arguments on my own time to work on a crypter. I always specify whether or not the code I am sharing is part of a homework assignment because helping with homework may go against some people's ethics. I have post history with homework assignments I've worked on as proof of this.
  12. I see it now, I was trying to write the user input to the pointer, when I should have been writing to the variable that the pointer was pointing to, right? I think I fixed the code, and I added another function to print out the word backwards, but I'm not sure if I'm correctly understanding how to use the pointer in this function either. Here's the code: #include<stdio.h> #include<stdlib.h> main() { int getString(char *characterArray); void reverse(int charCount, char *characterArray); int charCount = 0; char characterArray[100]; /* Max buffer size for the array is 100 */ int maxChar; printf("Maximum number of characters for this run? "); scanf("%d", &maxChar); printf("\n Enter no more than %d characters: ", maxChar); charCount = getString(characterArray)-1; printf("\n You entered %d characters.\n", charCount); reverse(charCount, characterArray); } int getString(char *characterArray) { int charCount = 0; scanf(" %s%n", characterArray, &charCount); return charCount; } void reverse(int charCount, char *characterArray) { int i; for(i = charCount - 1; i < 0; i--) printf("%s", &characterArray[i]); } This code is printing out: it seems to start on the right side but starts spamming out random letters from the string that shouldn't be printing out. I'm not sure why its doing this unless it is a 2 dimensional array with multiple string arrays in the array.
  13. Hi, I am writing a C program that will take a certain number of characters (a word), print them backwards, and then tell the user whether or not the word is a palindrome. I ran into trouble with the scanf() function when trying to scan user input to a char variable after previously scanning into an int variable. After a quick Google search, I found out the problem was that I was not putting a space before the %c in my scanf function. But even after I put the space, the program would still loop infinitely, and I'm not sure whether it is a problem with my recursive string function or the scanf function I am using. Here is the code: #include<stdio.h> #include<stdlib.h> main() { int getString(); int maxChar; printf("Maximum number of characters for this run? "); scanf("%d", &maxChar); printf("\nEnter no more than %d characters: ", maxChar); getString(); printf("\n"); } int getString() { char c; scanf(" %c", &c); if(c != '\n') { getString(); printf("%c", c); } } As this is a homework assignment for a C class, I am not allowed to use strlen() or any other special functions. I can only use functions that I define myself (besides basic ones like printf() and scanf()). The program I have right now will take a string that the user enters and print it backwards, but only if I get rid of the scanf for the maxChar variable. Here is what the normal output looks like when it is working properly: And here it is with the new scanf function: The program will get hung here, I assume looping infinitely for some reason. If i remove the space in front of the %c in the getString() funciton, the program will crash after the max characters input like so: I assume this is not a crash but rather the program finishing after the scanf function in the getString() function reads the whitespace from the previous scanf and then cancels the if statement and prints nothing, terminating the program. I am not sure how to get around this, as we are not supposed to keep the main function relatively empty and use our own defined functions to scan user input. EDIT: I've been messing around with the program a bit just trying to get an external function which can read user input and then store it in the string array, however I have been getting a segmentation fault when I run the program. Here is the code: #include <stdio.h> main() { int getString(int maxCharacters, char characterArray[100]); int reverse(); int isPalindrome(); int maxCharacters = 0; char characterArray[100]; char programLoop = 'y'; do { printf("Maximum number of characters for this run?\n"); scanf("%d", & maxCharacters); printf("The maximum number of characters is %d\n", maxCharacters); getString(maxCharacters, characterArray); printf("Run the program again? (y/n)\n"); scanf(" %c", &programLoop); } while(programLoop == 'y'); } int getString(int maxCharacters, char characterArray[100]) { printf(" Enter no more than %d characters: ", maxCharacters); scanf(" %s", &characterArray); printf("%s\n", characterArray); } The result looks like this: I feel like its a problem in a way I am passing the characterArray array as an argument for the getString() function, but I'm not sure what I am doing wrong... Any advice would be greatly appreciated, Thanks!
  14. Haha well I'm not sure how the compiler for my class is but we are using the ANSI 90 version of C so it's pretty old Now I just have to finish my array assignment before tonight without using if statements and using only one variable besides arrays ¯\_(ツ)_/¯
  15. Oh that would have made it a lost easier in the first place! I messed around with do while before but I haven't really used it in any homework yet as I'm more comfortable with the normal while loops
  16. No problem, either way it was a very simple and understandable mistake, I just kept thinking it was a problem with the loops themselves
  17. Yeah I just realized that, thank you though!
  18. No I appreciate you not just telling me, I think I will learn better from learning how to troubleshoot code. But I think I understand. I had the printf("Enter an integer:\n"); scanf("%d", &user_input); in the while loop before the if statements in the loop actually started. So after it did the counting for the overall variables, which were done correctly, it would grab a different value for user_input from the user in the middle of the loop before the first value could be ran through the if statements. So it would never count the first value you enter at the beginning of the loop because it gets overwritten halfway through.. am I correct?
  19. I'm using the number 0... So from my understanding: if(number < 0) count++; And say for example I pass the number 3 through the loop. The compiler should take the number -3 and ask if(-3 < 0) count++; Since -3 is lower than 0, the variable count should be increased by 1. I'm not sure why I would need to use a different number besides 0 to check whether the integer is negative or positive... is there a thing with C where it treats 0 differently? Or maybe I'm just misunderstanding you...
  20. Are you sure its not counting the last number? 0 is supposed to terminate the loop, so it isn't supposed to count that... in this screenshot you can see I use the numbers 1 and 2, and it counts only 1 for positive numbers when the result should be 2, and it does not recognize 1 as the smallest. I thought it was only a problem with the negatives but I'm not so sure now. It correctly gets the total sum as 3, but only in the overall lines, in the positive if loop it still only counts the number 2, and not the number 1 EDIT: forgot to add screenshot
  21. I just tried that, but I might be misunderstanding what you are trying to say... Here is the code that I tried after your instruction: I moved the nonzero_count to the end of the while statement but I am still getting the same issue
  22. Hi, I had a homework assignment due a few months ago that i got a mostly good grade on, however there was one problem with my code. It would never count the first negative number I entered, but it would count all the ones after that. I'm not sure how to fix this, and I didn't get any feedback from my instructor. Here is the code: #include<stdio.h> main() { int user_input = 0; int total_sum = 0; int positive_sum = 0; int negative_sum = 0; int nonzero_count = 0; int positive_count = 0; int negative_count = 0; int largest = 0; int smallest = 0; int largest_positive = -65535; int smallest_positive = 65535; int largest_negative = -65535; int smallest_negative = 65535; float average = 0; float average_positive = 0; float average_negative = 0; printf("Enter an integer:\n"); scanf("%d", &user_input); while(user_input != 0) { nonzero_count++; total_sum = total_sum + user_input; average = (float)total_sum/(float)nonzero_count; if(user_input > largest) largest = user_input; if(user_input < smallest) smallest = user_input; printf("Enter an integer:\n"); scanf("%d", &user_input); if(user_input > 0) { positive_count++; positive_sum = positive_sum + user_input; average_positive = (float)positive_sum/(float)positive_count; if(user_input > largest_positive) { largest_positive = user_input; } if(user_input < smallest_positive) { smallest_positive = user_input; } } else if(user_input < 0) { negative_count++; negative_sum = negative_sum + user_input; average_negative = (float)negative_sum/(float)negative_count; if(user_input > largest_negative) { largest_negative = user_input; } if(user_input < smallest_negative) { smallest_negative = user_input; } } } printf("Overall:\n"); printf("Count is %d, sum is %d, maximum is %d, minimum is %d, average is %f\n\n", nonzero_count, total_sum, largest, smallest, average); printf("Positives:\n"); printf("Count is %d, sum is %d, maximum is %d, minimum is %d, average is %f\n\n", positive_count, positive_sum, largest_positive, smallest_positive, average_positive); printf("Negatives:\n"); printf("Count is %d, sum is %d, maximum is %d, minimum is %d, average is %f\n\n", negative_count, negative_sum, largest_negative, smallest_negative, average_negative); } My problem happens when trying to do the math on counting the number of negative numbers. It will always ignore the first negative number but count the rest, except in the overall counter. Here is an example of what I'm talking about: And with an extra negative number: Here it is with the full set of numbers I was instructed to use: I'm not sure if it's a problem with the placement or types of my if statements, but it's the only thing I can think of that would affect whether a number would get evaluated in the loop. Any advice would be greatly appreciated! Thanks! EDIT: To be more exact about the problem, the sum, maximum and minimum, and average seem to be working as intended in the negative loop, the only thing that is working incorrectly is the counting in the negative loop. It will count all of the negative numbers except the first one entered.
  23. Hi, I recently had an assignment where I needed to take a range of numbers, find which ones were prime, and then give the prime factorization of the non prime numbers. I got a relatively good grade on the assignment, but I missed out on a few points for not having asterisks in between the prime factorization. I want to figure out how to fix this but I am a bit stumped. Here is the function in question: int factor(int test_num) { int potential_factor; printf("The factorization of %d is: \n", test_num); for(potential_factor = 2; potential_factor <= test_num;) { if(test_num % potential_factor == 0) { printf("%d ", potential_factor); test_num = test_num / potential_factor; } else ++potential_factor; } } This has an output like so: I want to make it so instead of "2 2" it would look like "2 * 2" And also for higher numbers, like 196 for example, the factorization should look like "2 * 2 * 7 * 7" How would I go about doing this without adding an extra asterisk at the end of the last number? Thanks! P.S. I can add the entirety of the code if you would like to look it over.
  24. Thanks for the help! However, I'm using ANSI C (I think version 90.x) instead of C++, and there are no boolean expressions like True or False in this version.. I could set up a flag with a 1 or 0 variable instead maybe? Also, I rewrote the code a bit and was able to fix everything but I don't think it is as efficient as your method. Here it is: #include<stdio.h> main() { int counter = 0; int lower_bound, upper_bound, num_counter, i; printf("Lower bound? "); scanf("%d", &lower_bound); printf("Upper bound? "); scanf("%d", &upper_bound); num_counter = lower_bound; while (num_counter <= upper_bound) { if (num_counter <= 1) { printf(" %d is not prime\n", num_counter); } else if (num_counter == 2) { counter++; printf(" %d is prime\n", num_counter); } else if (num_counter == 3) { counter++; printf(" %d is prime\n", num_counter); } else if (num_counter > 1) { for (i = 2; i <= num_counter / 2; i++) { if ((num_counter % i) == 0) { printf(" %d is not prime\n", num_counter); break; } if ((num_counter % i) != 0) { counter++; printf(" %d is prime\n", num_counter); break; } } } num_counter = num_counter + 1; } int num_range = upper_bound - lower_bound + 1; float prime_density = (float)counter / (float)num_range; printf("Over the interval from "); printf("%d", lower_bound); printf(" to "); printf("%d,", upper_bound); printf(" inclusive, the prime density was "); printf("%f\n", prime_density); } I removed the break in the first if statement and changed the printf lines to printf(" %d is/ is not prime\n", variable). Also, the counter is to count the number of prime numbers it finds, and then divides that by the range of numbers checked to get the density of prime numbers in a given range. I also wrote in 2 more else if statements to automatically say prime if the number is a 2 or a 3. I will try to implement your method with a flag with a 1 or 0 value and see if I can make it more efficient. Thanks!
  25. I am writing a program in C that checks for prime numbers within a range of numbers. The program seems to be working fine, but there are a couple problems that I haven't been able to figure out. Here is the code: #include<stdio.h> main() { int counter = 0; int lower_bound, upper_bound, num_counter, i; printf("Lower bound?\n"); scanf("%d", &lower_bound); printf("Upper bound?\n"); scanf("%d", &upper_bound); num_counter = lower_bound; while (num_counter <= upper_bound) { if (num_counter <= 1) { printf("%d", num_counter); printf(" is not prime\n"); break; } else if (num_counter > 1) { for (i = 2; i <= num_counter / 2; i++) { if ((num_counter % i) == 0) { printf("%d", num_counter); printf(" is not prime\n"); break; } if ((num_counter % i) != 0) { counter = counter + 1; printf("%d", num_counter); printf(" is prime\n"); break; } } } num_counter = num_counter + 1; } printf("%d\n", counter); int num_range = upper_bound - lower_bound + 1; printf("%d\n", num_range); float prime_density = counter / num_range; printf("%f\n", prime_density); /*printf("Over the interval from "); printf("%d", lower_bound); printf(" to "); printf("%d", upper_bound); printf(" inclusive, the prime density was "); printf("%f", prime_density);*/ } The first problem I have is that the program does not like the number 3 as a lower_bound value, here is the output when I try a range from 3 to 6: Lower bound? 3 Upper bound? 6 4 is not prime 5 is prime 6 is not prime 1 4 0.000000 It will function almost perfectly but will not print out anything for 3 or add one to the counter. The next problem I have is with a float called prime_density. I have the string stuff commented out so I could try and figure out why it kept printing out 0.000000. As you can see above, the result for prime_density should be 0.25 if the counter is at 1 and the num_range is at 4, but it just prints 0.000000 every time, even with number ranges that don't include 3: Lower bound? 7 Upper bound? 10 7 is prime 8 is not prime 9 is prime 10 is not prime 2 4 0.000000 Any help or advice to fix this would be greatly appreciated!
×