Search code examples
pythonpython-2.7python-3.xpacketspyshark

How to print all destination ports and source ports in the PCAP file?


import pyshark
pkts = pyshark.FileCapture("test.pcap")


for p in pkts:
      print

I am trying to print all destination ports and source ports in the PCAP file. How could I do it?


Solution

  • import pyshark
    
    pkts = pyshark.FileCapture('cap.pcap')
    
    for p in pkts:
        if hasattr(p, 'tcp'):
            print(p.tcp.srcport + ' -- ' + p.tcp.dstport)
        if hasattr(p, 'udp'):
            print(p.udp.srcport + ' -- ' + p.udp.dstport)