首页 / 操作系统 / Linux / Python多进程示例:ping检测局域网机器
使用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.htmPython文件处理:读取文件 http://www.linuxidc.com/Linux/2013-08/88496.htm如何发布自定义的Python模块 http://www.linuxidc.com/Linux/2013-08/88495.htmPython爬虫多线程抓取代理服务器 http://www.linuxidc.com/Linux/2013-07/87289.htmPython中re(正则表达式)模块详解 http://www.linuxidc.com/Linux/2013-08/88588.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里