使用python多进程模块multiprocessing来对局域网内机器进行ping检测
#!/usr/bin/env pythonimport multiprocessing
import subprocess
host_list = ['172.16.13.11','172.16.13.12','172.16.13.13', '1.2.3.4']
if len(host_list) > 30:
process_number = 30
else:
process_number = len(host_list)
def ping_host(ipaddr):
if subprocess.call('ping -c1 -W 1 %s > /dev/null' % ipaddr, shell=True) == 0:
print '%s is OK' % ipaddr
else:
print '%s is DOWN' % ipaddr
pool = multiprocessing.Pool(processes=process_number)
for ip in host_list:
pool.apply_async(ping_host,(ip,))
pool.close()
pool.join()
本文出自 “运维之路” 博客,请务必保留此出处http://netexr.blog.51cto.com/2480285/1307916