我写了一个程序,可以在迭代中收集数据包中的数据。直到几天前,它还运行得很好。编辑:已解决。我将IP保存为常量,它覆盖了IP。
from scapy.all import *
import requests
import socket
ROUND = 2
IP = 'localhost'
PORT = 80
SERVER_ADDRESS = (IP,PORT)
def get_packet_size(packet):
return len(packet)
def find_port(packet,ip):
if packet[IP].dst == ip:
if TCP in packet:
return packet[TCP].sport
else:
return packet[UDP].sport
else:
if UDP in packet:
return packet[TCP].dport
else:
return packet[UDP].dport
def check_traffic(packet , ip):
if packet[IP].dst == ip:
return False
else:
return True
def find_country(packet, ip):
request = "http://freegeoip.net/json/"+ip
response = requests.get(request)
real_response = response.text
real_response = real_response.split(",")
country_full = real_response[2]
country_full = country_full.split(":")
country = country_full[1]
country = country[1:len(country) - 1]
print(country)
return str(country)
def find_ip(packet):
name = socket.gethostname()
ip_of_agent = socket.gethostbyname(name)
if(packet[IP].dst != ip_of_agent):
return packet[IP].dst
else:
return packet[IP].src
def work_on_packets(packets):
packet_dic = {}
#ip_dic = []
i = 0 # num of packet in iteration.
for packet in packets:
print("\n")
packet_ip = find_ip(packet)
print(packet_ip)
country = find_country(packet,packet_ip)
print(country)
is_coming_traffic = check_traffic(packet,packet_ip) # True if coming , False if outer traffic.
port = find_port(packet,packet_ip)
print(port)
packet_size = get_packet_size(packet)
print(packet_size)
packet_dic["Packet "+str(i)] = [packet_ip,country,is_coming_traffic,port,packet_size]
i = i + 1
#send_data(packet_dic)
def is_IP(packet):
return ((UDP in packet or TCP in packet) and IP in packet)
def main():
print("Starting sniff..")
while(True):
packets = sniff(lfilter = is_IP )
work_on_packets(packets)
main()但现在它就是不起作用。输出总是这样,仅此而已:
WARNING: No route found for IPv6 destination :: (no default route?). This affects only IPv6
Starting sniff..它背后的问题可能是什么?任何帮助都是很棒的!
发布于 2017-06-20 23:05:24
这只是一个警告,告诉您没有任何用于IPV6的默认路由,对于没有IPV6的系统来说,这是完全正常的。这应该不会影响您的IPV4数据包程序。
下面是禁用它的方法
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)https://stackoverflow.com/questions/44656550
复制相似问题