Counting and printing packets using pyshark


#!/usr/bin/env python3

import pyshark

packets_array = []

def counter(*args):
        packets_array.append(args[0])


def count_packets():
        cap = pyshark.FileCapture('http.cap', keep_packets=False)
        cap.apply_on_packets(counter, timeout=10000)
        return len(packets_array)

print("Packets number:"+str(count_packets()))

for packet in packets_array:
        print(packet)
        break


$python3  count_packets.py 
Packets number:43
Packet (Length: 62)
Layer ETH:
	Destination: fe:ff:20:00:01:00
	Address: fe:ff:20:00:01:00
	.... ..1. .... .... .... .... = LG bit: Locally administered address (this is NOT the factory default)
	.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
	Source: 00:00:01:00:00:00
	.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
	.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
	Type: IPv4 (0x0800)
	Address: 00:00:01:00:00:00
Layer IP:
	0100 .... = Version: 4
	.... 0101 = Header Length: 20 bytes (5)
	Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
	0000 00.. = Differentiated Services Codepoint: Default (0)
	.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
	Total Length: 48
	Identification: 0x0f41 (3905)
	Flags: 0x40, Don't fragment
	0... .... = Reserved bit: Not set
	.1.. .... = Don't fragment: Set
	..0. .... = More fragments: Not set
	...0 0000 0000 0000 = Fragment Offset: 0
	Time to Live: 128
	Protocol: TCP (6)
	Header Checksum: 0x91eb [validation disabled]
	Header checksum status: Unverified
	Source Address: 145.254.160.237
	Destination Address: 65.208.228.223
Layer TCP:
	Source Port: 3372
	Destination Port: 80
	Stream index: 0
	Conversation completeness: Incomplete (0)
	TCP Segment Len: 0
	Sequence Number: 0    (relative sequence number)
	Sequence Number (raw): 951057939
	Next Sequence Number: 1    (relative sequence number)
	Acknowledgment Number: 0
	Acknowledgment number (raw): 0
	0111 .... = Header Length: 28 bytes (7)
	Flags: 0x002 (SYN)
	000. .... .... = Reserved: Not set
	...0 .... .... = Nonce: Not set
	.... 0... .... = Congestion Window Reduced (CWR): Not set
	.... .0.. .... = ECN-Echo: Not set
	.... ..0. .... = Urgent: Not set
	.... ...0 .... = Acknowledgment: Not set
	.... .... 0... = Push: Not set
	.... .... .0.. = Reset: Not set
	.... .... ..1. = Syn: Set
	Expert Info (Chat/Sequence): Connection establish request (SYN): server port 80
	Connection establish request (SYN): server port 80
	Severity level: Chat
	Group: Sequence
	.... .... ...0 = Fin: Not set
	TCP Flags: ··········S·
	Window: 8760
	Calculated window size: 8760
	Checksum: 0xc30c [unverified]
	Checksum Status: Unverified
	Urgent Pointer: 0
	Options: (8 bytes), Maximum segment size, No-Operation (NOP), No-Operation (NOP), SACK permitted
	TCP Option - Maximum segment size: 1460 bytes
	Kind: Maximum Segment Size (2)
	Length: 4
	MSS Value: 1460
	TCP Option - No-Operation (NOP)
	TCP Option - SACK permitted
	Timestamps
	Time since first frame in this TCP stream: 0.000000000 seconds
	Time since previous frame in this TCP stream: 0.000000000 seconds
	Kind: No-Operation (1)
	Kind: No-Operation (1)
	Kind: SACK Permitted (4)
	Length: 2
	TCP Option - No-Operation (NOP)

$


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: