To ping several hosts at once you could use subprocess.Popen()
:
#!/usr/bin/env python3
import os
import time
from subprocess import Popen, DEVNULL
p = {} # ip -> process
for n in range(1, 100): # start ping processes
ip = "127.0.0.%d" % n
p[ip] = Popen(['ping', '-n', '-w5', '-c3', ip], stdout=DEVNULL)
#NOTE: you could set stderr=subprocess.STDOUT to ignore stderr also
while p:
for ip, proc in p.items():
if proc.poll() is not None: # ping finished
del p[ip] # remove from the process list
if proc.returncode == 0:
print('%s active' % ip)
elif proc.returncode == 1:
print('%s no response' % ip)
else:
print('%s error' % ip)
break
If you can run as a root you could use a pure Python ping script or scapy
:
from scapy.all import sr, ICMP, IP, L3RawSocket, conf
conf.L3socket = L3RawSocket # for loopback interface
ans, unans = sr(IP(dst="127.0.0.1-99")/ICMP(), verbose=0) # make requests
ans.summary(lambda (s,r): r.sprintf("%IP.src% is alive"))