使用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 的下載地址:請點這裡