Scapy sniff() 在子类化 threading.Thread() 的类中 [英] Scapy sniff() in a class that subclassess threading.Thread()
问题描述
Scapy sniff() 函数有一个奇怪的问题.
I've got a strange issue with the Scapy sniff() function.
这是我班级的样子:
from scapy.all import *
import sys
import datetime
import Queue
from threading import Thread
class packet_sniffer(Thread):
def __init__(self,pass_queue):
super(packet_sniffer,self).__init__()
print 'Packet sniffer started'
self.queue=pass_queue
self.device_dict={}
self.not_an_ap={}
def PacketHandler(self,pkt):
if pkt.haslayer(Dot11):
sig_str = -(256-ord(pkt.notdecoded[-4:-3]))
mac_addr=""
ssid=""
try:
mac_addr=pkt.addr2
ssid=pkt.info
except:
return
if self.device_dict.has_key(pkt.addr2) and pkt.info!=self.device_dict[pkt.addr2]:
output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
print output
self.device_dict.pop(pkt.addr2)
self.not_an_ap[pkt.addr2]=pkt.info
self.queue.put(output)
elif pkt.info=="" or pkt.info=="Broadcast":
output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
print output
self.queue.put(output)
else:
pot_mac=self.not_an_ap.get(pkt.addr2)
if pot_mac == None:
self.device_dict[pkt.addr2]=pkt.info
def run(self):
sniff(iface="mon.wlan0",prn=self.PacketHandler)
当我从我的线程管理器类调用此代码时它不起作用:
This code doesn't work when I call it from my Thread manager class:
当我说它不起作用时,我的意思是嗅探要么无法运行,要么没有调用 PacketHandler.没有错误信息输出,程序的其余部分继续正常
When I say it doesn't work, I mean that the sniff is either not operational or isn't calling PacketHandler. No error messages are outputted, and the rest of the program continues as normal
currentQueue=Queue()
#object setup
print'Initialising sniffer'
packet_sniffer_instance=packet_sniffer(currentQueue)
packet_sniffer_instance.daemon=True
packet_sniffer_instance.start()
time.sleep(1)
print'Finished initialising sniffer'
我看了这篇文章后加入了睡眠功能:Scapy 无法嗅探
I included the sleep function after looking at this post: Scapy fails to sniff
但是,当我将嗅探调用移动到 __init__()
函数时,它可以工作,但是由于 packet_sniffer 类无限地停留在 __init__ 中,因此无法调用后续线程()
函数.
However, when I move my sniff call to the __init__()
function, it works, but then no subsequent threads can be called due to the packet_sniffer class being infinitely stuck in the __init__()
function.
就 Python 而言,我是一个相当新的程序员(总体上不是一个新程序员,我有很多经验),所以我可能在做一些非常基本的错误.
I'm fairly new programmer when it comes to python (not a new programmer overall, i have lots of experience), so I'm probably doing something really basic wrong.
TIA
詹姆斯.
推荐答案
似乎只需重新排列类,使 run
方法就在 __init__()
下方法解决了这个问题.我也不再使用线程类,而是使用了多进程类,它建立在 Thread 类的基础上,但允许更高的并发性.
It seems that simply rearranging the class so that the run
method came just under the __init__()
method fixed the issue. I also stopped using the thread class and used the multiprocess class, which builds on the Thread class but allows greater concurrency.
最终的类是这样的:
from scapy.all import *
import sys
import datetime
import Queue
from multiprocessing import Process
class packet_sniffer(Process):
def __init__(self,pass_queue):
super(packet_sniffer,self).__init__()
print 'Packet sniffer started'
#self.target=self.monitor()
self.queue=pass_queue
self.device_dict={}
self.not_an_ap={}
print 'END'
def run(self):
sniff(iface="en1",prn=self.PacketHandler)
def PacketHandler(self,pkt):
if(pkt.haslayer(ARP)):
print pkt.src
if pkt.haslayer(Dot11):
sig_str = -(256-ord(pkt.notdecoded[-4:-3]))
mac_addr=""
ssid=""
try:
mac_addr=pkt.addr2
ssid=pkt.info
except:
return
if self.device_dict.has_key(pkt.addr2) and pkt.info!=self.device_dict[pkt.addr2]:
output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
print output
self.device_dict.pop(pkt.addr2)
self.not_an_ap[pkt.addr2]=pkt.info
self.queue.put(output)
elif pkt.info=="" or pkt.info=="Broadcast":
output= "DIS MAC:%s RSSI:%s " %(pkt.addr2,sig_str)
print output
self.queue.put(output)
else:
pot_mac=self.not_an_ap.get(pkt.addr2)
if pot_mac == None:
self.device_dict[pkt.addr2]=pkt.info
我不完全确定为什么在这种情况下方法的安排会有所不同.
I'm not entirely sure why the arrangement of the methods makes a difference in this instance.
这篇关于Scapy sniff() 在子类化 threading.Thread() 的类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!