I have done a few modifications in the above code with multithreading in python 2.7:
import subprocess,os,threading,time
import Queue
lock=threading.Lock()
_start=time.time()
def check(n):
with open(os.devnull, "wb") as limbo:
ip=n
result=subprocess.Popen(["ping", "-n", "2", "-w", "300", ip],stdout=limbo, stderr=limbo).wait()
with lock:
if not result:
print ip, "active"
else:
print ip, "Inactive"
def threader():
while True:
worker=q.get()
check(worker)
q.task_done()
q = Queue.Queue()
for x in range(255):
t=threading.Thread(target=threader)
t.daemon=True
t.start()
ip = ["13.45.23.523", "13.35.23.523","23.23.56.346"]
for worker in ip:
q.put(worker)
q.join()
print("Process completed in: ",time.time()-_start)