[英]Scapy variable sniff stop
i found a similar problem: ( Instance variables not being updated Python when using Multiprocessing ), but still do not know the solutionn for my task.我发现了一个类似的问题:( 使用 Multiprocessing 时未更新 Python 实例变量),但仍然不知道我的任务的解决方案。
The task is to stop a scapy sniff function after the completness of a testskript.任务是在测试脚本完成后停止 scapy 嗅探功能。 the running duration of single testscripts can vary greatly (from some seconds till hours).
单个测试脚本的运行持续时间可能会有很大差异(从几秒到几小时)。 My sniff function runs in a separate threat.
我的嗅探功能在单独的威胁中运行。 The testscript calls an init Funktion in the beginning which calls the sniff Function from an other modul.
测试脚本在开始时调用了一个 init Funktion,它从另一个模块调用了嗅探函数。
@classmethod
def SaveFullTrafficPcap(self, TestCase, Termination):
try:
Full_Traffic = []
PktList = []
FullPcapName = Settings['GeneralSettings']['ResultsPath']+TestCase.TestCaseName +"Full_Traffic_PCAP.pcap"
#while Term.Termination < 1:
Full_Traffic = sniff(lfilter=None, iface=str(Settings['GeneralSettings']['EthInterface']), store=True, prn = lambda x: Full_Traffic.append(x), count=0, timeout=Term.Termination)
print(Full_Traffic)
wrpcap(FullPcapName, Full_Traffic)
except(Exception):
SYS.ABS_print("No full traffic PCAP file wirtten!\n")
At the end of the testscript an exit function is called.在测试脚本的末尾调用一个退出函数。 In the exit function I set Term.Termination parameter to 1 and wait for 5 sec, but it doesnt work.
在退出函数中,我将 Term.Termination 参数设置为 1 并等待 5 秒,但它不起作用。 The sniff function is stoped by the system and i get no file"FullPCAPName" If count or timeout get a value, the code works without problemms and i get my FullPCAPName file with he complet traffic on my Interface.
嗅探功能被系统停止,我没有得到文件“FullPCAPName”如果计数或超时得到一个值,代码工作没有问题,我得到我的 FullPCAPName 文件,他在我的接口上完成了流量。
Have anybody hinds how i can stopt the sniff function regulary after finisching the testscript?有没有人阻止我在完成测试脚本后如何定期停止嗅探功能?
Use of the stop_filter command as specified here worked for me. 使用此处指定的stop_filter命令对我有用。 I've duplicated HenningCash's code below for convenience:
为方便起见,我在下面复制了HenningCash的代码:
import time, threading
from scapy.all import sniff
e = threading.Event()
def _sniff(e):
a = sniff(filter="tcp port 80", stop_filter=lambda p: e.is_set())
print("Stopped after %i packets" % len(a))
print("Start capturing thread")
t = threading.Thread(target=_sniff, args=(e,))
t.start()
time.sleep(3)
print("Try to shutdown capturing...")
e.set()
# This will run until you send a HTTP request somewhere
# There is no way to exit clean if no package is received
while True:
t.join(2)
if t.is_alive():
print("Thread is still running...")
else:
break
print("Shutdown complete!")
However, you still have to wait for a final packet to be sniffed, which might not be ideal in your scenario. 但是,您仍然需要等待最终数据包被嗅探,这在您的方案中可能并不理想。
now i solved the problem with global variables. 现在我解决了全局变量的问题。 It is not nice, but it works well.
这不好,但效果很好。
Nevertheless I am interested in a better solution for the variable sniff stop. 尽管如此,我对可变嗅探停止更好的解决方案感兴趣。
stop_var = ""
def stop():
global stop_var
stop_var.stop()
def start():
"""
your code
"""
global stop_var
stop_var = AsyncSniffer(**arg)
stop_var=start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.