2017-06-20 99 views
0

我写了一个程序,它从迭代中收集数据包中的数据。直到几天前它工作正常。 编辑:已解决。我将IP保存为一个常量并覆盖IP。我的scapy嗅探器不工作?

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.. 

它背后的问题是什么?任何帮助都很棒!

回答

0

这只是一个警告,告诉您不要有任何IPV6的默认路由,对于没有IPV6的系统是完全正常的。这不应该影响您的IPV4数据包的程序。

这里是你如何可以禁用它

import logging 
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) 
+0

不,那不是问题。我正在将“本地主机”保存为常量而不是IP,并将其覆盖。 – MichaelFel123