Westfall_T Westfall_T - 3 months ago 12
Python Question

Sending multiple pings with Python

How can I ping

192.168.0.1
-
192.168.0.254
all at once? Trying to make the script run faster as it takes several minutes to finish.

import os
import subprocess


ip = raw_input("IP Address? ")
print "Scanning IP Address: " + ip

subnet = ip.split(".")

FNULL = open(os.devnull, 'w')

for x in range(1, 255):
ip2 = subnet[0]+"."+ subnet[1] +"."+ subnet[2] +"."+ str(x)
response=subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip2], stdout=FNULL, stderr=subprocess.STDOUT).wait()
if response == 0:
print ip2, 'is up!'
else:
print ip2, 'is down!'

Answer

Look at the method you use to get a response:

response=subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip2], stdout=FNULL, stderr=subprocess.STDOUT).wait()

most importantly, the .wait() at the end means your program will wait until the process finishes.

You can start 255 processes at once (though you might want to start smaller chunks for the case of sanity) by putting the result of Popen (and not wait) into a list:

processes = []
for ip8 in range(1, 255):
    ip32 = subnet[0]+"."+ subnet[1] +"."+ subnet[2] +"."+ str(ip8)
    process = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip32], stdout=FNULL, stderr=subprocess.STDOUT)
    processes.append(process)

You can then go through each and every process and wait until they finish:

for process, ip8 in zip(processes, range(1, 255)):
    ip32 = subnet[0]+"."+ subnet[1] +"."+ subnet[2] +"."+ str(ip8)
    response = process.wait()
    if response == 0:
        print("%s is up!" % (ip32))
    else:
        print("%s is down!" % (ip32))