我使用scapy在python中创建了一个数据包嗅探器,但是它仍然存在于多线程中。
def sniffer(ip):
filter_str = "icmp and host " + ip
packets=sniff(filter=filter_str,count=20)
status= False
for p in packets:
packet_load=str(p['Raw'].load)
if packet_load.find("@@")!= -1:
status=True
log_thread = Thread(target=logger,args=(packets,))
log_thread.start()
log_thread.join()
break
if status==True:
print "Suspicious Packets sniffed!!"
user_ip = raw_input("Do you want to continue sniffing???(y/n)")
while 1:
if user_ip=="y" or user_ip=="Y":
new_thread = Thread(target=sniffer, args=(ip,))
new_thread.start()
new_thread.join()
else:
#need somthing to quit the program
return
在这里,我的嗅探器一次嗅探20个数据包并等待用户输入以进一步嗅探。 但是,如果用户输入“n”作为输入,则程序将挂起。理想情况下,如果用户输入'n',我希望程序退出。我能在这里知道我做错了吗?
答案 0 :(得分:1)
while 1
很少是一个不错的选择。请尝试使用标志:
leaving = False
while not leaving:
user_ip = raw_input("Do you want to continue sniffing???(y/n)")
if user_ip.lower() == 'y':
new_thread = Thread(target=sniffer, args=(ip,))
new_thread.start()
new_thread.join()
elif user_ip.lower() == 'n':
print "Leaving sniffer"
leaving = True
return