Aug-11-2023, 03:07 PM
I have a script which i thought was working correctly until i purposely added some invalid IP's into my list so that i can validate the results file.
The "response" value is always this = Access denied. Option -c requires administrative privileges. and never has the true ping response. So now i have a solution that i thought was working and in fact not really working as all results returned are "Up" based on the condition.
So i have 2 questions:
#1 - what can be done to prevent that error, since i do not have admin rights on my work pc.
#2 - If there is no way around that error based on the script code below, is there any other method of pinging over 4000 IP's in a timely manner?
This script run against all 4000 ip's in under 4 minutes.. which currently is ok processing time.. IF it can be done more efficiently I'm all for improvements. I have another post about trying the icmp method, but cant seem to get that working and dont want to mess with the below yet.
The above message is returned in the "response" but still allows the script to run and return results.
response = os.popen(f"ping -c 1 {ip} ").read() using this method returns the message above and takes about 4 minutes to run thru 4000 ip's, but returns false positives.
response = os.popen(f"ping /n 1 {ip} ").read() using this method no message and takes about 21 minutes to run thru 4000 ip's but this time returns valid results.
The "response" value is always this = Access denied. Option -c requires administrative privileges. and never has the true ping response. So now i have a solution that i thought was working and in fact not really working as all results returned are "Up" based on the condition.
So i have 2 questions:
#1 - what can be done to prevent that error, since i do not have admin rights on my work pc.
#2 - If there is no way around that error based on the script code below, is there any other method of pinging over 4000 IP's in a timely manner?
This script run against all 4000 ip's in under 4 minutes.. which currently is ok processing time.. IF it can be done more efficiently I'm all for improvements. I have another post about trying the icmp method, but cant seem to get that working and dont want to mess with the below yet.
The above message is returned in the "response" but still allows the script to run and return results.
response = os.popen(f"ping -c 1 {ip} ").read() using this method returns the message above and takes about 4 minutes to run thru 4000 ip's, but returns false positives.
response = os.popen(f"ping /n 1 {ip} ").read() using this method no message and takes about 21 minutes to run thru 4000 ip's but this time returns valid results.
import os import time from datetime import datetime now = datetime.now() # current date and time date_time = now.strftime("%m/%d/%Y,%H:%M:%S") start = time.time() # DIRECTORY WHERE ALL IP LISTS RESIDE directory = 'Lists' # RETURN LIST OF ALL FILES IN THE "DIRECTORY" PROVIDED ABOVE. for entry in os.scandir(directory): # THIS WILL LOOP THROUGH ALL TEXT FILES IN DIRECTORY if entry.is_file() and entry.name.endswith('.txt'): # THIS WILL ONLY LOOK FOR AND RETURN FILES WITH IP_LIST IN THE NAME SINCE THOSE ARE THE ONES WITH THE SPECIFIC IP'S if 'ip_list' in entry.name: # FILE PATH FOR EACH FILE TO PROCESS(EX. Lists/Register_ip_list.txt) pt = directory + '/' + entry.name # THIS WILL PARSE OUT THE VALUE BEFORE THE FIRST '_' TO SET THE VARIABLE AND USE LATER FOR SAVING THE RESULTS. parts = entry.name.split('_') # OPEN EACH TEXT FILE THAT CONTAINS IPS TO PROCESS with open(pt) as file: park = file.read() park = park.splitlines() #print(" {park} \n") # THIS IS TO CLEAR THE FILE BEFORE WRITING TO IT(WONT NEED THIS ONCE WE START INSERTING THE DATE INTO A DATABASE) file_to_clear = open("Lists/" + parts[0] +"_ip_output.txt",'w') file_to_clear.close() # PING EACH IP IN FILE LIST for ip in park: response = os.popen(f"ping -c 1 {ip} ").read() # SAVE PING STATUS INTO FILE FOR DASHBOARD (IP,STATUS,DATETIME) if("Request timed out." or "unreachable") in response: #print(response) f = open("Lists/" + parts[0] +"_ip_output.txt","a") f.write(str(ip) + ',down,'+ date_time +'\n') f.close() else: #print(response) f = open("Lists/" + parts[0] +"_ip_output.txt","a") f.write(str(ip) + ',up,'+ date_time +'\n') f.close() # PRINT COMPLETE RESULTS TO CONSOLE WINDOW (DEBUGGING PURPOSES) # with open("Lists/" + parts[0] +"_ip_output.txt") as file: # output = file.read() # f.close() # print(output) # THIS CLEARS THE FILE AFTER ITS WRITTEN TO THE CONSOLE #with open("Lists/ip_output.txt","w") as file: # pass # CALCULATED THE RUNTIME OF THE SCRIPT (FUTURE UPDATES TO INCLUDE CAPTURING RUNTIMES IN SEPARATE FILE FOR REVIEW, TO INCLUDE FILENAME, ROW COUNT AND DURATION) end = time.time() print(end - start)