N
N
Northxstar2019-02-20 20:14:52
Python
Northxstar, 2019-02-20 20:14:52

Pyshark: how to serialize a package to json?

There is a simple pyshark sniffer that outputs the captured packet directly to bash:

import pyshark
capture = pyshark.LiveCapture(interface='ens33')
while(1):
 capture.sniff(packet_count=1)
 for packet in capture:
  print (packet)

Task: serialize this output to json (then transfer it and parse it on the other side)
Pyshark itself has a packet_from_json_packet(json_pkt) method, but I can’t do the opposite

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LODIII, 2019-02-22
@Northxstar

looked more closely at the output.
this is 99% typical yaml file
installed Pyaml
and tried
or in the interpreter

import yaml

with open("tshark.yml", 'r') as stream:
    data_loaded = yaml.safe_load(stream)

quarreled at lines 15 and 39 that map could not
remove the second ones in them:
and everything was converted
{
    "Packet (Length": "90)",
    "Layer ETH": {
        "Destination": "c4:6e:1f:07:3f:42",
        "Address": "00:0c:29:ef:ff:9c",
        ".... ..0. .... .... .... .... = LG bit": "Globally unique address (factory default)",
        ".... ...0 .... .... .... .... = IG bit": "Individual address (unicast)",
        "Source": "00:0c:29:ef:ff:9c",
        "Type": "IPv4 (0x0800)"
    },
    "Layer IP": {
        "0100 .... = Version": 4,
        ".... 0101 = Header Length": "20 bytes (5)",
        "Differentiated Services Field": 16,
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question