python多线程ping和arpping扫描工具

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#/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!"

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:邀请码生成器Java代码

下一篇:GPS 经纬度转换为 经过旋转后的平面坐标