0

So i'm trying to print a table, where the header is fixed, and the rows containing of packet information is constantly added, instead of re-printing the same table with new packets.

import pyshark
import sys
import os
from prettytable import PrettyTable

cap = pyshark.LiveRingCapture(interface='eth0')
cap.sniff_continuously()

myTable = PrettyTable(['PROTOCOL','SOURCE IP:','PORT','DESTINATION IP:'])

ip=None
print(myTable)
def print_packets(cap):
    for pkt in cap:
        try :
            myTable.add_row([pkt.highest_layer,pkt.ip.addr,pkt.tcp.port ,pkt.ip.dst])
            print(myTable)
        except AttributeError:
            continue

def main():
    cap.apply_on_packets(print_packets(cap))

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(130)
        except SystemExit:
            os._exit(130)

This is what it looks like :

enter image description here

I Know that the fault in my code is that the for loop, tells the code to re-print the entire table, but I have no clue on doing what I actually want. Can anybody give some advice ? Thanks

apantilie
  • 1
  • 1

0 Answers0