加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python多线程ping和arpping扫描工具

发布时间:2020-12-17 17:16:04 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #/usr/bin/env python#--encoding=UTF-8--#a simpl ping scaner import subprocessfrom threading import Threadfrom Queue import Queueimport re nu

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#/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,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,out_queue))
    worker.setDaemon(True)
#        worker.Daemon = True
    worker.start()
 
print "[*]Main Thread Waiting."
in_queue.join()
out_queue.join()
 
print "[*]Done!"

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读