多线程测试 系统hosts文件主机网络情况,多线程hosts,[Python]代码#_


[Python]代码

#_*_coding:utf-8_*_'''程序:  快速ping 作用测试系统hosts配置主机是否可达程序:ping_host.pydate:2011-05-11开发: gyhong gyh9711测试环境包括:支持ip4  原因,正则表达式检查win xp   ---测试成功linux : ubuntn redhat  ---测试成功unix: solairs hpunix aix   ---除hpunix 外,测试成功freebsd: 测试失败,单独python测试成功,但集成后测试失败,原因查找中 2011-05-12soliar aix 测试ok2011-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 OK123.234.20.36 is ERROR123.234.20.36 is ERRORadministrator@nagios:/win$ python ping_hosts.py127.0.0.1 is OK127.0.1.1 is OK192.168.20.35 is OK192.168.20.100 is ERROR-bash-3.00$ unameSunOS-bash-3.00$ python ping_hosts.py  test.log   # 测试文件中所列出主机是否网络可达,192.168.1.10 is OK192.168.1.11 is OK192.168.1.1 is OK192.168.1.221 is ERROR192.168.1.2 is ERROR192.168.1.3 is ERROR192.168.1.2 is ERROR-bash-3.00$ python ping_hosts.py 192.168.2.3 is OK192.168.1.10 is OK127.0.0.1 is OK'''import os,sysimport refrom threading import Threadhost=[]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()

评论关闭