Sending multiple pings with Python
<p>How can I ping <code>192.168.0.1</code> - <code>192.168.0.254</code> all at once? Trying to make the script run faster as it takes several minutes to finish. </p>

<pre><code>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!'
</code></pre>

<p>Look at the method you use to get a response:</p>

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

<p>most importantly, the <code>.wait()</code> at the end means your program will wait until the process finishes.</p>

<p>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:</p>

<pre><code>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)
</code></pre>

<p>You can then go through each and every process and wait until they finish:</p>

<pre><code>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))
</code></pre>

python