多线程测试 系统hosts文件主机网络情况

2019-06-19 20:03栏目:技术创新

程序功能:多线程测试 系统hosts文件中主机是否网络可达

编写原因,维护的一台网管服务器,连接节点4百多个,节点经常会有变动,因此需要定期维护hosts配置文件。

#_*_coding:utf-8_*_
'''
程序:  快速ping 作用测试系统hosts配置主机是否可达
程序:ping_host.py
date:2011-05-11
开发: gyhong gyh9711
测试环境包括:
支持ip4  原因,正则表达式检查
win xp   ---测试成功
linux : ubuntn redhat  ---测试成功
unix: solairs hpunix aix   ---除hpunix 外,测试成功
freebsd: 测试失败,单独python测试成功,但集成后测试失败,原因查找中 2011-05-12

soliar aix 测试ok
2011-05-12 HP-UX szssrv B.11.11 U 9000/800 3378642797 unlimited-user license  没安装python,无法测试

>"C:\\Python26\\pythonw.exe" -u "ping_hosts.py"
127.0.0.1 is OK
123.234.20.36 is ERROR
123.234.20.36 is ERROR

administrator@nagios:/win$ python ping_hosts.py
127.0.0.1 is OK
127.0.1.1 is OK
192.168.20.35 is OK
192.168.20.100 is ERROR

-bash-3.00$ uname
SunOS
-bash-3.00$ python ping_hosts.py  test.log   # 测试文件中所列出主机是否网络可达,
192.168.1.10 is OK
192.168.1.11 is OK
192.168.1.1 is OK
192.168.1.221 is ERROR
192.168.1.2 is ERROR
192.168.1.3 is ERROR
192.168.1.2 is ERROR
-bash-3.00$ python ping_hosts.py 
192.168.2.3 is OK
192.168.1.10 is OK
127.0.0.1 is OK

'''
import os,sys
import re
from threading import Thread

host=[]
if len(sys.argv) == 2:
    #配置文件配置信息检查有待改进 2011-05-12
    if os.path.isfile(sys.argv[1]):
        l_f=open(sys.argv[1],"r").readlines()
        for i in l_f:
            if re.search(r"^[1-9]",i):
                host.append(i.replace("\n","").replace("\r\n",""))
    else: 
        print("Usage: python ping_hosts.py host_config_filename")
        sys.exit(1)
elif len(sys.argv) == 1:
    if os.name == "nt":
        f=open("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","r").readlines()
        for l_i in f:
            if re.search(r"^[1-9]",l_i):
                host.append(l_i.replace(" ","\t").split("\t")[0])   # 问题,需要解决 \t  空格问题
    elif os.name == "posix":  #linux系统  solairs x86
        f=open("/etc/hosts","r").readlines()
        for l_i in f:
            if re.search(r"^[1-9]",l_i):
                host.append(l_i.replace(" ","\t").split("\t")[0])
else:
    print("Usage: python ping_hosts.py")
    sys.exit(1)

class PING(Thread):
    def __init__(self,ip):
        Thread.__init__(self)
        self.ip=ip
    def run(self):
        if os.name== "nt":  #windows操作系统
            output=os.popen("ping -n 1 %s" % (self.ip)).read().split("\r\n")
            if "    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)," in output:
                print("%s is OK" %(self.ip))
                #~ print output
            else:
                print("%s is ERROR" %(self.ip))
        elif os.name=="posix":
            if os.uname()[0] == "SunOS":   #x86 solaris 操作系统
                output=os.popen("ping %s 1" % (self.ip)).read()
                if re.search("alive",output):
                    print("%s is OK" %(self.ip))
                    #~ print output
                else:
                    print("%s is ERROR" %(self.ip))
            elif os.uname()[0] == "FreeBSD":   # FreeBSD
                output=os.popen("ping -c 1 -W 2 %s" % (self.ip)).read().split("\n")
                if "1 packets transmitted, 1 packets received, 0.0% packet loss, 1 packets out of wait time" in output:
                    print("%s is OK" %(self.ip))
                else:
                    print("%s is ERROR" %(self.ip))
            elif os.uname()[0] == "AIX":           #通用linux操作系统
                output=os.popen("ping -c 1 -w 2 %s" % (self.ip)).read().split("\n")
                if "1 packets transmitted, 1 packets received, 0% packet loss" in output:
                    print("%s is OK" %(self.ip))
                    #~ print output
                else:
                    print("%s is ERROR" %(self.ip))
            elif os.uname()[0] == "Linux":           #通用linux操作系统
                output=os.popen("ping -c 1 -W 2 %s" % (self.ip)).read().split("\n")
                if "1 packets transmitted, 1 received, 0% packet loss, time 0ms" in output:
                    print("%s is OK" %(self.ip))
                    #~ print output
                else:
                    print("%s is ERROR" %(self.ip))

#~ host=["192.168.1.1","192.168.1.123","192.168.2.1","192.168.1.1","192.168.1.123"]

report_ok=[]
report_error=[]

#多线程同时执行
T_thread=[]

for i in host:
    t=PING(i)
    T_thread.append(t)
for i in range(len(T_thread)):
    T_thread[i].start()
#该片段来自于http://outofmemory.cn

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:多线程测试 系统hosts文件主机网络情况