1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

process route data

This commit is contained in:
Kelly Brazil
2019-11-07 18:33:51 -08:00
parent 89973f4162
commit a55493da0f

View File

@ -3,10 +3,52 @@
Usage: Usage:
specify --route as the first argument if the piped input is coming from route specify --route as the first argument if the piped input is coming from route
Examples:
Example: $ route -ee | jc --route -p
[
{
"destination": "default",
"gateway": "gateway",
"genmask": "0.0.0.0",
"flags": "UG",
"metric": 100,
"ref": 0,
"use": 0,
"iface": "ens33",
"mss": 0,
"window": 0,
"irtt": 0
},
{
"destination": "172.17.0.0",
"gateway": "0.0.0.0",
"genmask": "255.255.0.0",
"flags": "U",
"metric": 0,
"ref": 0,
"use": 0,
"iface": "docker",
"mss": 0,
"window": 0,
"irtt": 0
},
{
"destination": "192.168.71.0",
"gateway": "0.0.0.0",
"genmask": "255.255.255.0",
"flags": "U",
"metric": 100,
"ref": 0,
"use": 0,
"iface": "ens33",
"mss": 0,
"window": 0,
"irtt": 0
}
]
$ route | jc --route -p $ route -ee | jc --route -p -r
[ [
{ {
"destination": "default", "destination": "default",
@ -16,7 +58,10 @@ $ route | jc --route -p
"metric": "100", "metric": "100",
"ref": "0", "ref": "0",
"use": "0", "use": "0",
"iface": "ens33" "iface": "ens33",
"mss": "0",
"window": "0",
"irtt": "0"
}, },
{ {
"destination": "172.17.0.0", "destination": "172.17.0.0",
@ -26,7 +71,10 @@ $ route | jc --route -p
"metric": "0", "metric": "0",
"ref": "0", "ref": "0",
"use": "0", "use": "0",
"iface": "docker0" "iface": "docker",
"mss": "0",
"window": "0",
"irtt": "0"
}, },
{ {
"destination": "192.168.71.0", "destination": "192.168.71.0",
@ -36,13 +84,47 @@ $ route | jc --route -p
"metric": "100", "metric": "100",
"ref": "0", "ref": "0",
"use": "0", "use": "0",
"iface": "ens33" "iface": "ens33",
"mss": "0",
"window": "0",
"irtt": "0"
} }
] ]
""" """
import jc.utils import jc.utils
def process(proc_data):
'''schema:
[
{
"destination": string,
"gateway": string,
"genmask": string,
"flags": string,
"metric": integer,
"ref": integer,
"use": integer,
"mss": integer,
"window": integer,
"irtt": integer,
"iface": string
}
]
'''
for entry in proc_data:
int_list = ['metric', 'ref', 'use', 'mss', 'window', 'irtt']
for key in int_list:
if key in entry:
try:
key_int = int(entry[key])
entry[key] = key_int
except (ValueError):
entry[key] = None
return proc_data
def parse(data, raw=False, quiet=False): def parse(data, raw=False, quiet=False):
# compatible options: linux, darwin, cygwin, win32, aix, freebsd # compatible options: linux, darwin, cygwin, win32, aix, freebsd
compatible = ['linux', 'aix', 'freebsd'] compatible = ['linux', 'aix', 'freebsd']
@ -56,4 +138,9 @@ def parse(data, raw=False, quiet=False):
cleandata = data.splitlines()[1:] cleandata = data.splitlines()[1:]
headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h] headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h]
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:]) raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
return [dict(zip(headers, r)) for r in raw_data] raw_output = [dict(zip(headers, r)) for r in raw_data]
if raw:
return raw_output
else:
return process(raw_output)