[Python] 多线程ping和arpping扫描工具 →→→→→进入此内容的聊天室

来自 , 2020-10-26, 写在 Python, 查看 123 次.
URL http://www.code666.cn/view/78573683
  1. #/usr/bin/env python
  2. #--encoding=UTF-8--
  3. #auth@:xfk
  4. #blog@:blog.sina.com.cn/kaiyongdeng
  5. #data@:2012-04-15
  6. #a simpl ping scaner
  7.  
  8. import subprocess
  9. from threading import Thread
  10. from Queue import Queue
  11. import re
  12.  
  13. num_ping_threads = 3
  14. num_arp_threads = 3
  15. in_queue = Queue()
  16. out_queue = Queue()
  17. #ips = ["10.65.10.50","10.65.10.80"]
  18. ips = ["你要扫描的ip范围"]
  19.  
  20. def ping_scan(i,iq,oq):
  21.         while True:
  22.                 ip = iq.get()
  23.                 print "[*]Thread %s: Pinging %s" % (i,ip)
  24.                 ret = subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = subprocess.STDOUT)
  25.                 if ret == 0:
  26.                         print "[*]%s: is alive." % ip
  27.                         oq.put(ip)
  28.                 else:
  29.                         print "[*]%s: did not respond" % ip
  30.                 iq.task_done()
  31.  
  32. def arping_scan(i,oq):
  33.         while True:
  34.                 ip = oq.get()
  35.                 p = subprocess.Popen("arping -c 1  %s" % ip,shell = True,stdout = subprocess.PIPE)
  36.                 out = p.stdout.read()
  37.                 result = out.split()
  38.                 pattern = re.compile(".*:.*:.*")
  39.                 macaddr = None
  40.                 for item in result:
  41.                         if re.search(pattern,item):
  42.                                 macaddr = item
  43.                         print "[*]IP Address: %s | Mac Address: %s" % (ip,macaddr)
  44.                 oq.task_done()
  45.  
  46. for ip in ips:
  47.         in_queue.put(ip)
  48.  
  49. for i in range(num_ping_threads):
  50.         worker = Thread(target = ping_scan,args = (i,in_queue,out_queue))
  51.         worker.setDaemon(True)
  52.         worker.start()
  53.  
  54. for i in range(num_arp_threads):
  55.         worker = Thread(target = arping_scan,args = (i,out_queue))
  56.         worker.setDaemon(True)
  57. #        worker.Daemon = True
  58.         worker.start()
  59.  
  60. print "[*]Main Thread Waiting."
  61. in_queue.join()
  62. out_queue.join()
  63.  
  64. print "[*]Done!"
  65.  
  66. #//python/1178

回复 "多线程ping和arpping扫描工具"

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

captcha