dongfangho 发表于 2018-8-7 07:34:39

python和shell的ping对比

#!/usr/bin/python  
#_*_coding:utf-8_*_
  
import os
  
import time
  
from threading import Thread
  
class PING(Thread):
  
def __init__(self,ip):
  
Thread.__init__(self)
  
self.ip=ip
  
def run(self):
  
if os.name=="nt":
  
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 使用" % self.ip
  
else:
  
print "\033[32m%s 未使用\033[0m" % self.ip
  
elif os.uname() == "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 使用\r" % self.ip
  
else:
  
print "\033[32m%s 未使用\r\033[0m" % self.ip
  
else:
  
print ("%s is ERROR" %(self.ip))
  
#多线程同时执行
  
T_thread=[]
  
Ip_addr=[]
  
aa=raw_input('\033[31m input ping range,exp:192.168.0.0,192.168.0.10 \033[0m\n>>>')
  
aa1=aa.split('.')
  
ip1="%s.%s.%s." %(aa1,aa1,aa1)
  
ip2=int(aa1)
  
ip2+=1
  
aa2=aa1.split(',')
  
for i in range(int(aa2),ip2):
  
Ip_addr.append(ip1+str(i))
  
for i in Ip_addr:
  
t=PING(i)
  
T_thread.append(t)
  
for i in range(len(T_thread)):
  
T_thread.start()
页: [1]
查看完整版本: python和shell的ping对比