Aug-09-2023, 01:41 AM
Im trying to tweak this logic a bit to return some additional information or even look at threading it so it runs a little faster.
I have a list of 3036 ip addresses i need to ping in a timely manner about 3 times aday.
Currently the below run at just under 4 minutes, which is better than the macro someone wrote in excel to accomplish the same thing(takes 30 minutes)
File to contain:
Ip, Ping Time(ms) value, timestamp of run(this needs to be the same for all records at time of each run)
Example:
8.8.8.8, 20, 8:50pm
Also the above when run, returns this message:
Access denied. Option -c requires administrative privileges.
Even though it displays this on the console the script still runs and returns what i need, but not sure how to get around that OR if that is impacting the run time of the script. I mean 4minutes is good compared to 30minutes, but if i can improve that time even more it would be great.
Any suggestions or examples on how to tweak this to capture the additional data? Subprocess? Threading?
I have a list of 3036 ip addresses i need to ping in a timely manner about 3 times aday.
Currently the below run at just under 4 minutes, which is better than the macro someone wrote in excel to accomplish the same thing(takes 30 minutes)
import os
import time
start = time.time()
with open("ip_list.txt") as file:
park = file.read()
park = park.splitlines()
print(" {park} \n")
# ping for each ip in the file
for ip in park:
response = os.popen(f"ping -c 1 {ip} ").read()
# Pinging each IP address 4 times
#saving some ping output details to output file
if("Request timed out." or "unreachable") in response:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' link is down'+'\n')
f.close()
else:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' is up '+'\n')
f.close()
# print output file to screen
with open("ip_output.txt") as file:
output = file.read()
f.close()
print(output)
#with open("ip_output.txt","w") as file:
#pass
end = time.time()
print(end - start)Right now this outputs the IP address and the status, but what we would like to capture for historical purposes is the following in a CSV file to consume elsewhere. File to contain:
Ip, Ping Time(ms) value, timestamp of run(this needs to be the same for all records at time of each run)
Example:
8.8.8.8, 20, 8:50pm
Also the above when run, returns this message:
Access denied. Option -c requires administrative privileges.
Even though it displays this on the console the script still runs and returns what i need, but not sure how to get around that OR if that is impacting the run time of the script. I mean 4minutes is good compared to 30minutes, but if i can improve that time even more it would be great.
Any suggestions or examples on how to tweak this to capture the additional data? Subprocess? Threading?
