使用多线程,在linux服务器上秒出结果,主要是快,只支持一个c段之内的地址,例如python ipcheck.py 192.168.0.1 192.168.0.254
#-*-coding:utf-8-*-
#****************************************************************#
# ScriptName: ipcheck.py
# Author: kf
# Create Date: 2019-05-26 18:38
# Modify Author: kf
# Modify Date: 2019-05-26 18:38
# Function:
#***************************************************************#
import threading,sys,os,re,time
inused = []
notused = []
inusedcount = 0
notusedcount = 0
lock = threading.Lock()
class ipcheck(threading.Thread):
def __init__(self,ip,cmd):
threading.Thread.__init__(self)
self.ip = ip
self.cmd = cmd
def run(self):
code = os.system(self.cmd)
global inusedcount
global notusedcount
global inused
global notused
if code == 0:
lock.acquire()
inused.append(self.ip)
inusedcount += 1
lock.release()
else:
lock.acquire()
notused.append(self.ip)
notusedcount += 1
lock.release()
if len(sys.argv) != 3:
print(len(sys.argv))
print "Please input the range of ip to check !!!!"
print "e.g : ipcheck.py 192.168.1.1 192.168.1.99"
exit(1)
#else:
# if sys.argv
start = sys.argv[1].split('.')
end = sys.argv[2].split('.')
if start[0] == end[0] and start[1] == end[1] and start[2] == end[2]:
netbit = "%s.%s.%s"%(start[0],start[1],start[2])
x = int(start[3])
y = int(end[3]) + 1
for hostbit in range(x,y):
ip = netbit + "." + str(hostbit)
cmd = "ping -c 1 -W 1 " + ip + " > /dev/null"
ipcheck(ip,cmd).start()
time.sleep(1)
while True:
if threading.active_count() <= 1:
print "in using :"
print inused
print inusedcount
print "-------------------------------------------------------"
print "not used :"
print notused
print notusedcount
break