0

I am making a packet capture tool using pyshark and python argparse module. so I want to make a feature that only print the imcp when the command -ieth0 -I is used. but I am getting error.

as well the code only show the tcp and udp protocol . it does not show other protocol like imp, arp, etc while capturing the packet.

my code is below.

import argparse
import pyshark
import time
#import psutil

parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output', help = 'to save the file in pcap format')
parser.add_argument('-c','--count', help = 'to print limited number of packet',type=int)
parser.add_argument('-d','--dns', help = 'to print dns packet', required=False, action='store_ture)
parser.add_argument('-I', '--ICMP', required=False, action='store_true', help = 'to capture icm packet)
parser.add_argument('-t', '--tcp',required=False, action='store_true', help='to capture tcp packet)
parser.add_argument('-u','--udp',required=False, action='store_true', help='to cpature only udp)
parser.add_argument('-v', '--verbose', help='be more specific', default=False, action='store_true)
parser.add_argument('-i', '--interface',metavar="", type=str, required=True, help='network interface)
args = parser.parse_args()

if args.dns:
   print ("-----------------------------DNS packets only---------------------")
   capture = pyshark.LiveCapture(interface=args.interface, bpf_filter='icmp')

#elif args.output:
 #  print ("---------------------------capture and save the packet-----------------")
   #capture = pyshark.LiveCapture(output_file=args.output)

elif args.count:
   capture = pyshark.LiveCapture(interface=args.interface)
   for packet in capture.sniff_continuously(packet_count=args.count):
       if packet == args.count:
          break

elif args.ICMP:
    print ("--------------------------ICMP packets only----------------------------")
    capture = pyshark.LiveCapture(interface=args.interface, bpf_filter='icmp')

elif args.tcp:
    print ("--------------------------TCP packets only-----------------------------")
    capture = pyshark.LiveCapture(interface=args.interface, bpf_filter='tcp')

elif args.udp:
    print ("------------------------UDP Packets only -----------------------------")
    capture = pyshark.LiveCapture(interface=args.interface, bpf_filter='udp port 53')

else:
    capture = pyshark.LiveCapture(interface=args.interface, output_file=args.output)

packet_list =[]
for packet in capture.sniff_continuously():
    packet_list.append(packet)
    localtime = time.asctime(time.localtime(time.time()))
    protocol = packet.transport_layer
    src_addr = packet.ip.src
    src_port = packet[protocol].srcport
    dst_addr = packet.ip.dst
    dst_port = packet[protocol].dstport
    print(localtime,"\t",protocol,"\t", src_addr,"\t", src_port,"\t", dst_addr, "\t", dst_port)
    if args.verbose:
        print (packet.show())


output

--------------------------ICMP packets only----------------------------
Traceback (most recent call last):
  File "/home/kali/help.py", line 54, in <module>
    src_port = packet[protocol].srcport
  File "/home/kali/.local/lib/python3.10/site-packages/pyshark/packet/packet.py", line 50, in __getitem__
    if layer.layer_name == item.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

0 Answers0