Python Forum
Need some guidance on a script to ping a list of ip's
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some guidance on a script to ping a list of ip's
#1
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)

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?
Reply


Messages In This Thread
Need some guidance on a script to ping a list of ip's - by cubangt - Aug-09-2023, 01:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  non-stop ping script kucingkembar 1 2,191 Aug-23-2022, 06:29 AM
Last Post: menator01
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 5,323 May-17-2021, 11:29 AM
Last Post: throwaway34
  Ping command using python 3.6.5 Martin2998 6 28,463 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha
  Your Guidance caslor 1 2,923 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Create a program that PING a list of IPs skaailet 7 10,339 Mar-26-2020, 10:46 PM
Last Post: snippsat
  ping with output jacklee26 1 6,226 Nov-28-2019, 01:01 PM
Last Post: Axel_Erfurt
  PING PONG GAME akea 0 6,438 May-08-2019, 04:30 PM
Last Post: akea
  Ping Code Problem MTom5 1 3,302 Sep-04-2018, 09:58 PM
Last Post: MTom5
  ping and pong run both well sylas 1 3,934 Sep-24-2017, 05:14 PM
Last Post: sylas
  Ping Sweep help The501stOfNoobPy 5 8,647 Jun-26-2017, 10:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020