玩蛇网提供最新Python编程技术信息以及Python资源下载!

python多线程ping及arpping扫描工具的实现

python 培训

这是一个python多线程ping和arpping扫描工具实现的源码。

这里还有待修改的地方,例如:希望能扫描一个地址范围,使用IPy模块可以实现对ip地址的处理,实现扫描一个网段的ip,不用像现在实现的这么笨拙。应该有更巧妙的方法。

代码中需要用到的python模块方法有:subprocess、python Queuepython re

#/usr/bin/env python
#--encoding=UTF-8--

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()

#www.iplaypython.com
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!"

python ping相关文章推荐:
Python pexpect包应用之快速多线程ping测试
python实现多线程Ping网段方法源码

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2262.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-06-14 14:38 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部