Search code examples
python-2.7network-programmingscapypacket-sniffers

Python Scapy output to txt file


I would like to output just IP.dst to txt file, But I get all the packet info including Ether, src, etc

from scapy.all import *
import time
import os
file = open("newfile.txt","w")
t = '%IP.dst%'
p = sniff(filter="ip", prn=lambda x:x.sprintf(t), count=10)
file.write(str(p))
time.sleep(1)
os.system("cls")

Sample output of txt file

Ether dst=f4:ce:46:5c:bf:f8 src=30:10:b3:24:63:b6 type=0x800 /|IP version=4L ihl=5L


Solution

  • @TeckSupport how about if you set up a function that pulls and writes the IP.dst:

    from scapy.all import *
    
    fob = open("IP.txt","w")
    
    def ip_dst(pkt):
            fob.write(pkt[IP].dst+'\n')
    
    sniff(filter='ip',count=10,prn=ip_dst)
    fob.close()
    

    Is this what you were looking to do?