import subprocess as p
import threading
import time
processes=[]
def startPing(ip):
fuck = p.Popen('ping -c 2 %s' % ip,shell=True,stdin=p.PIPE,stdout=p.PIPE,stderr=p.PIPE,encoding='gbk')
result = fuck.stdout.read()
if '0 packets received' in result:
print(ip,'is Down')
else:
print(ip,'this is Up')
def main():
for i in range(1,255):
t=threading.Thread(target=startPing,args=('10.8.1.'+str(i),))
t.start()
processes.append(t)
for n in processes:
n.join()
print('all done')
if __name__ == '__main__':
main()
运行效果如下:
image.png