使用Python多进程模块multiprocessing来对局域网内机器进行ping检测
#!/usr/bin/env python
import 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()
荐阅读:
Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
Python文件处理:读取文件 http://www.linuxidc.com/Linux/2013-08/88496.htm
如何发布自定义的Python模块 http://www.linuxidc.com/Linux/2013-08/88495.htm
Python爬虫多线程抓取代理服务器 http://www.linuxidc.com/Linux/2013-07/87289.htm
Python中re(正则表达式)模块详解 http://www.linuxidc.com/Linux/2013-08/88588.htm
Python 的详细介绍:请点这里
Python 的下载地址:请点这里
