securecoding
securecoding

Reputation: 2903

print scapy sniff output to file

I have created a sniffer in scapy and I want the packets captured by scapy to be written onto a file for further analysis?

def sniffer(ip):
    filter_str = "icmp and host " + ip
    packets=sniff(filter=filter_str,count=20)
    f = open('log.txt',"a")
    #f.write(packets)

The last line of code does not work. Is there any way I could do this?

Upvotes: 0

Views: 4453

Answers (1)

Gary Kerr
Gary Kerr

Reputation: 14420

f.write expects a character buffer, but you supply it with a Sniffed object which is the result of calling sniff. You can, very simply, do the following:

f.write(str(packets))

This should work. But it probably won't display the information exactly as you would like it. You are going to have to do more work collecting information from packets as strings before you write to f.

Upvotes: 1

Related Questions