2019-11-13 07:45:37 -08:00
|
|
|
# jc.parsers.ss
|
|
|
|
jc - JSON CLI output utility ss Parser
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
specify --ss as the first argument if the piped input is coming from ss
|
|
|
|
|
2019-11-14 13:37:55 -08:00
|
|
|
Limitations:
|
2019-11-14 13:38:52 -08:00
|
|
|
Extended information options like -e and -p are not supported and may cause parsing irregularities
|
2019-11-14 13:37:55 -08:00
|
|
|
|
2019-11-13 07:45:37 -08:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ sudo ss -a | jc --ss -p
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"netid": "nl",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"channel": "rtnl:kernel"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "nl",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"pid": 893,
|
|
|
|
"channel": "rtnl:systemd-resolve"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
2019-11-14 09:42:44 -08:00
|
|
|
...
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
2019-11-14 09:42:44 -08:00
|
|
|
"netid": "p_raw",
|
2019-11-13 07:45:37 -08:00
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"link_layer": "LLDP",
|
|
|
|
"interface": "ens33"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "u_dgr",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
|
|
|
"local_port": "93066",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "0",
|
|
|
|
"path": "/run/user/1000/systemd/notify"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "u_seq",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 128,
|
|
|
|
"local_port": "20699",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "0",
|
|
|
|
"path": "/run/udev/control"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
|
|
|
...
|
2019-11-14 09:42:44 -08:00
|
|
|
{
|
|
|
|
"netid": "icmp6",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
|
|
|
"local_address": "*",
|
|
|
|
"local_port": "ipv6-icmp",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "ens33"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "udp",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
|
|
|
"local_address": "127.0.0.53",
|
|
|
|
"local_port": "domain",
|
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "lo"
|
|
|
|
},
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 128,
|
2019-11-14 09:42:44 -08:00
|
|
|
"local_address": "127.0.0.53",
|
|
|
|
"local_port": "domain",
|
2019-11-13 07:45:37 -08:00
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "lo"
|
|
|
|
},
|
2019-11-14 09:42:44 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 128,
|
|
|
|
"local_address": "0.0.0.0",
|
|
|
|
"local_port": "ssh",
|
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*"
|
|
|
|
},
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 128,
|
|
|
|
"local_address": "[::]",
|
|
|
|
"local_port": "ssh",
|
|
|
|
"peer_address": "[::]",
|
|
|
|
"peer_port": "*"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "v_str",
|
|
|
|
"state": "ESTAB",
|
|
|
|
"recv_q": 0,
|
|
|
|
"send_q": 0,
|
|
|
|
"local_address": "999900439",
|
|
|
|
"local_port": "1023",
|
|
|
|
"peer_address": "0",
|
|
|
|
"peer_port": "976",
|
|
|
|
"local_port_num": 1023,
|
|
|
|
"peer_port_num": 976
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
$ sudo ss -a | jc --ss -p -r
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"netid": "nl",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"channel": "rtnl:kernel"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "nl",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"pid": "893",
|
|
|
|
"channel": "rtnl:systemd-resolve"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
2019-11-14 09:42:44 -08:00
|
|
|
...
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
2019-11-14 09:42:44 -08:00
|
|
|
"netid": "p_raw",
|
2019-11-13 07:45:37 -08:00
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
2019-11-14 09:42:44 -08:00
|
|
|
"peer_address": "*",
|
|
|
|
"link_layer": "LLDP",
|
|
|
|
"interface": "ens33"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "u_dgr",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
|
|
|
"local_port": "93066",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "0",
|
|
|
|
"path": "/run/user/1000/systemd/notify"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "u_seq",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "128",
|
|
|
|
"local_port": "20699",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "0",
|
|
|
|
"path": "/run/udev/control"
|
2019-11-13 07:45:37 -08:00
|
|
|
},
|
|
|
|
...
|
2019-11-14 09:42:44 -08:00
|
|
|
{
|
|
|
|
"netid": "icmp6",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
|
|
|
"local_address": "*",
|
|
|
|
"local_port": "ipv6-icmp",
|
|
|
|
"peer_address": "*",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "ens33"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "udp",
|
|
|
|
"state": "UNCONN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
|
|
|
"local_address": "127.0.0.53",
|
|
|
|
"local_port": "domain",
|
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "lo"
|
|
|
|
},
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "128",
|
2019-11-14 09:42:44 -08:00
|
|
|
"local_address": "127.0.0.53",
|
|
|
|
"local_port": "domain",
|
2019-11-13 07:45:37 -08:00
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*",
|
|
|
|
"interface": "lo"
|
|
|
|
},
|
2019-11-14 09:42:44 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "128",
|
|
|
|
"local_address": "0.0.0.0",
|
|
|
|
"local_port": "ssh",
|
|
|
|
"peer_address": "0.0.0.0",
|
|
|
|
"peer_port": "*"
|
|
|
|
},
|
2019-11-13 07:45:37 -08:00
|
|
|
{
|
|
|
|
"netid": "tcp",
|
|
|
|
"state": "LISTEN",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "128",
|
|
|
|
"local_address": "[::]",
|
|
|
|
"local_port": "ssh",
|
|
|
|
"peer_address": "[::]",
|
|
|
|
"peer_port": "*"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"netid": "v_str",
|
|
|
|
"state": "ESTAB",
|
|
|
|
"recv_q": "0",
|
|
|
|
"send_q": "0",
|
|
|
|
"local_address": "999900439",
|
|
|
|
"local_port": "1023",
|
|
|
|
"peer_address": "0",
|
|
|
|
"peer_port": "976"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
## process
|
|
|
|
```python
|
|
|
|
process(proc_data)
|
|
|
|
```
|
|
|
|
|
|
|
|
Final processing to conform to the schema.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2019-11-13 08:04:40 -08:00
|
|
|
proc_data: (dictionary) raw structured data to process
|
2019-11-13 07:45:37 -08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
dictionary structured data with the following schema:
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"netid": string,
|
|
|
|
"state": string,
|
|
|
|
"recv_q": integer,
|
|
|
|
"send_q": integer,
|
|
|
|
"local_address": string,
|
|
|
|
"local_port": string,
|
|
|
|
"local_port_num": integer,
|
|
|
|
"peer_address": string,
|
|
|
|
"peer_port": string,
|
|
|
|
"peer_port_num": integer,
|
2019-11-14 09:42:44 -08:00
|
|
|
"interface": string,
|
|
|
|
"link_layer" string,
|
|
|
|
"channel": string,
|
|
|
|
"path": string,
|
|
|
|
"pid": integer
|
2019-11-13 07:45:37 -08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-11-14 09:42:44 -08:00
|
|
|
Information from https://www.cyberciti.biz/files/ss.html used to define field names
|
|
|
|
|
2019-11-13 07:45:37 -08:00
|
|
|
## parse
|
|
|
|
```python
|
|
|
|
parse(data, raw=False, quiet=False)
|
|
|
|
```
|
|
|
|
|
|
|
|
Main text parsing function
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
data: (string) text data to parse
|
|
|
|
raw: (boolean) output preprocessed JSON if True
|
|
|
|
quiet: (boolean) suppress warning messages if True
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
dictionary raw or processed structured data
|
|
|
|
|