Get info string from scapy packet

nik picture nik · Mar 26, 2015 · Viewed 12.9k times · Source

I am using scapy 2.3.1-dev non-interactively (i.e. as a library) in a tool I am building. I would like to get a string of human-readable information about a packet, such as you get from scapy.all.Packet.show(). I have tried using all three of the methods (packet.show(), packet.show2() and packet.display()) that provide the info, but none of these return anything, instead they print out the information that I want.

Also, the information returned by packet.__repr__() is not quite enough.

Are there any functions/methods that would return the nicely-formatted text the same way that e.g. packet.show() prints them? If not is there some way of capturing/intercepting the output of show(), before it gets printed to the console?

I am aware that I can do my own string formatting, using the info from packet.fields, but I am trying to avoid having to do that.

Answer

MrMeizhi picture MrMeizhi · Jul 18, 2017

You can use show() method by show(dump=True),then it will return string to you. Why Do I Know that,cause I read the code of show() method.

here is code:

def main():
    packet = scapy.rdpcap('1.pcap')
    for p in packet:
        a = p.show(dump=True)
        print type(a)
        print a
        exit(0)