[Python] 多线程Ping网段 →→→→→进入此内容的聊天室

来自 , 2021-04-03, 写在 Python, 查看 138 次.
URL http://www.code666.cn/view/9f4768b4
  1. import sys
  2. import subprocess
  3. from threading import Thread
  4. from Queue import Queue
  5.  
  6. if sys.hexversion < 0x02040000:
  7.         print >> sys.stderr, 'Your python version is too old (%s)' % \
  8.                                                         (sys.version.split()[0])
  9.         print >> sys.stderr, 'You need at least Python 2.4'
  10.         sys.exit(1)
  11.  
  12. class CreateThread(Thread):
  13.         def __init__(self, func, args, name=""):
  14.                 Thread.__init__(self)
  15.                 self.__name = name
  16.                 self.__func = func
  17.                 self.__args = args
  18.  
  19.         def run(self):
  20.                 apply(self.__func, self.__args)
  21.  
  22. class FPing(object):
  23.         def __init__(self, network, tnum):
  24.                 self.network = network
  25.                 self.tnum = tnum
  26.  
  27.         def ip_process(self, network):
  28.                 start = network.split('-')[0].split('.')[3]
  29.                 end = network.split('-')[1]
  30.                 ip = network.split('-')[0].split('.')[ : 3]
  31.                 ip_3 = '.'.join(ip)
  32.                 return (start, end, ip_3)
  33.        
  34.         def check_grama(self, network):
  35.                 check = network.find('-')
  36.                 if check == -1:
  37.                         self.usage()
  38.                         sys.exit(1)
  39.                        
  40.         def usage(self):
  41.                 print '''Usage: python fping.py ip-num
  42.                 example:
  43.                 python fping 192.168.1.1-255'''
  44.        
  45.         def ping_scan(self, num, iq, oq):
  46.                 while True:
  47.                         try:
  48.                                 ip = iq.get()
  49.                                 print "[*]Thread %s: Pinging %s" % (num, ip)
  50.                                 ret = subprocess.call("ping -c 1 -w 3 %s" % ip, shell = True,
  51.                                                         stdout = open('/dev/null','w'), stderr = subprocess.STDOUT)
  52.                                 if ret == 0:
  53.                                         print "[*]%s: is alive." % ip
  54.                                         oq.put(ip)
  55.                                 else:
  56.                                         print "[*]%s: did not respond" % ip
  57.                                 iq.task_done()
  58.                         except Exception:
  59.                                 print "Threading Exception !"
  60.                                 sys.exit(1)
  61.  
  62.         def active(self):
  63.                 worker_num = self.tnum
  64.                 in_q = Queue()
  65.                 out_q = Queue()
  66.  
  67.                 start, end, ip3 = self.ip_process(self.network)
  68.  
  69.                 for ip1 in xrange(int(start), int(end) + 1):
  70.                         ip = ip3 + '.' + str(ip1)
  71.                         in_q.put(ip)
  72.  
  73.                 for i in xrange(worker_num):
  74.                         worker = CreateThread(self.ping_scan, (i, in_q, out_q), self.ping_scan.__name__)
  75.                         worker.setDaemon(True)
  76.                         worker.start()
  77.                        
  78.                 print "[*]Main Thread Waiting."
  79.                 in_q.join()
  80.                 out_q.join()
  81.                 print "[*]Done!"
  82.  
  83. if __name__ == '__main__':
  84.         thread_worker_num = 10
  85.         fping = FPing(sys.argv[1], thread_worker_num)
  86.         if len(sys.argv) != 2:
  87.                 fping.usage()
  88.                 sys.exit(1)
  89.         fping.active()
  90.  
  91.    
  92. #//python/1181

回复 "多线程Ping网段"

这儿你可以回复上面这条便签

captcha