2017-06-20 95 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