python多线程ping和arpping扫描工具详解编程语言

#/usr/bin/env python 
#--encoding=UTF-8-- 
#a simpl ping scaner 
  
import subprocess 
from threading import Thread 
from Queue import Queue 
import re 
  
num_ping_threads = 3 
num_arp_threads = 3 
in_queue = Queue() 
out_queue = Queue() 
#ips = ["10.65.10.50","10.65.10.80"] 
ips = ["你要扫描的ip范围"] 
  
def ping_scan(i,iq,oq): 
    while True: 
        ip = iq.get() 
        print "[*]Thread %s: Pinging %s" % (i,ip) 
        ret = subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = subprocess.STDOUT) 
        if ret == 0: 
            print "[*]%s: is alive." % ip 
            oq.put(ip) 
        else: 
            print "[*]%s: did not respond" % ip 
        iq.task_done() 
  
def arping_scan(i,oq): 
    while True: 
        ip = oq.get() 
        p = subprocess.Popen("arping -c 1  %s" % ip,shell = True,stdout = subprocess.PIPE) 
        out = p.stdout.read() 
        result = out.split() 
        pattern = re.compile(".*:.*:.*") 
        macaddr = None 
        for item in result: 
            if re.search(pattern,item): 
                macaddr = item 
            print "[*]IP Address: %s | Mac Address: %s" % (ip,macaddr) 
        oq.task_done() 
  
for ip in ips: 
    in_queue.put(ip) 
  
for i in range(num_ping_threads): 
    worker = Thread(target = ping_scan,args = (i,in_queue,out_queue)) 
    worker.setDaemon(True) 
    worker.start() 
  
for i in range(num_arp_threads): 
    worker = Thread(target = arping_scan,args = (i,out_queue)) 
    worker.setDaemon(True) 
#        worker.Daemon = True 
    worker.start() 
  
print "[*]Main Thread Waiting." 
in_queue.join() 
out_queue.join() 
  
print "[*]Done!"

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/8256.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论