
{"id":19076,"date":"2022-08-07T20:27:55","date_gmt":"2022-08-07T14:57:55","guid":{"rendered":"https:\/\/beautifulworknew.wordpress.com\/?p=19076"},"modified":"2022-08-07T20:27:55","modified_gmt":"2022-08-07T14:57:55","slug":"counting-and-printing-packets-using-pyshark","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2022\/08\/07\/counting-and-printing-packets-using-pyshark\/","title":{"rendered":"Counting and printing packets using pyshark"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env python3\n\nimport pyshark\n\npackets_array = []\n\ndef counter(*args):\n        packets_array.append(args[0])\n\n\ndef count_packets():\n        cap = pyshark.FileCapture('http.cap', keep_packets=False)\n        cap.apply_on_packets(counter, timeout=10000)\n        return len(packets_array)\n\nprint(\"Packets number:\"+str(count_packets()))\n\nfor packet in packets_array:\n        print(packet)\n        break\n\n\n$python3  count_packets.py \nPackets number:43\nPacket (Length: 62)\nLayer ETH:\n\tDestination: fe:ff:20:00:01:00\n\tAddress: fe:ff:20:00:01:00\n\t.... ..1. .... .... .... .... = LG bit: Locally administered address (this is NOT the factory default)\n\t.... ...0 .... .... .... .... = IG bit: Individual address (unicast)\n\tSource: 00:00:01:00:00:00\n\t.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)\n\t.... ...0 .... .... .... .... = IG bit: Individual address (unicast)\n\tType: IPv4 (0x0800)\n\tAddress: 00:00:01:00:00:00\nLayer IP:\n\t0100 .... = Version: 4\n\t.... 0101 = Header Length: 20 bytes (5)\n\tDifferentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)\n\t0000 00.. = Differentiated Services Codepoint: Default (0)\n\t.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)\n\tTotal Length: 48\n\tIdentification: 0x0f41 (3905)\n\tFlags: 0x40, Don't fragment\n\t0... .... = Reserved bit: Not set\n\t.1.. .... = Don't fragment: Set\n\t..0. .... = More fragments: Not set\n\t...0 0000 0000 0000 = Fragment Offset: 0\n\tTime to Live: 128\n\tProtocol: TCP (6)\n\tHeader Checksum: 0x91eb [validation disabled]\n\tHeader checksum status: Unverified\n\tSource Address: 145.254.160.237\n\tDestination Address: 65.208.228.223\nLayer TCP:\n\tSource Port: 3372\n\tDestination Port: 80\n\tStream index: 0\n\tConversation completeness: Incomplete (0)\n\tTCP Segment Len: 0\n\tSequence Number: 0    (relative sequence number)\n\tSequence Number (raw): 951057939\n\tNext Sequence Number: 1    (relative sequence number)\n\tAcknowledgment Number: 0\n\tAcknowledgment number (raw): 0\n\t0111 .... = Header Length: 28 bytes (7)\n\tFlags: 0x002 (SYN)\n\t000. .... .... = Reserved: Not set\n\t...0 .... .... = Nonce: Not set\n\t.... 0... .... = Congestion Window Reduced (CWR): Not set\n\t.... .0.. .... = ECN-Echo: Not set\n\t.... ..0. .... = Urgent: Not set\n\t.... ...0 .... = Acknowledgment: Not set\n\t.... .... 0... = Push: Not set\n\t.... .... .0.. = Reset: Not set\n\t.... .... ..1. = Syn: Set\n\tExpert Info (Chat\/Sequence): Connection establish request (SYN): server port 80\n\tConnection establish request (SYN): server port 80\n\tSeverity level: Chat\n\tGroup: Sequence\n\t.... .... ...0 = Fin: Not set\n\tTCP Flags: \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7S\u00b7\n\tWindow: 8760\n\tCalculated window size: 8760\n\tChecksum: 0xc30c [unverified]\n\tChecksum Status: Unverified\n\tUrgent Pointer: 0\n\tOptions: (8 bytes), Maximum segment size, No-Operation (NOP), No-Operation (NOP), SACK permitted\n\tTCP Option - Maximum segment size: 1460 bytes\n\tKind: Maximum Segment Size (2)\n\tLength: 4\n\tMSS Value: 1460\n\tTCP Option - No-Operation (NOP)\n\tTCP Option - SACK permitted\n\tTimestamps\n\tTime since first frame in this TCP stream: 0.000000000 seconds\n\tTime since previous frame in this TCP stream: 0.000000000 seconds\n\tKind: No-Operation (1)\n\tKind: No-Operation (1)\n\tKind: SACK Permitted (4)\n\tLength: 2\n\tTCP Option - No-Operation (NOP)\n\n$\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/usr\/bin\/env python3 import pyshark packets_array = [] def counter(*args): packets_array.append(args[0]) def count_packets(): cap = pyshark.FileCapture(&#8216;http.cap&#8217;, keep_packets=False) cap.apply_on_packets(counter, timeout=10000) return len(packets_array) print(&#8220;Packets number:&#8221;+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 &#8230;. ..1. &#8230;. &#8230;. &#8230;. &#8230;. = LG bit: Locally administered address (this is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2022\/08\/07\/counting-and-printing-packets-using-pyshark\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Counting and printing packets using pyshark&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/19076"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=19076"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/19076\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=19076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=19076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=19076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}