From a55493da0f1457a46f29511e0835adfa9d748f7b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 7 Nov 2019 18:33:51 -0800 Subject: [PATCH] process route data --- jc/parsers/route.py | 99 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 6 deletions(-) diff --git a/jc/parsers/route.py b/jc/parsers/route.py index 4aa39a04..888fbb41 100644 --- a/jc/parsers/route.py +++ b/jc/parsers/route.py @@ -3,10 +3,52 @@ Usage: 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", @@ -16,7 +58,10 @@ $ route | jc --route -p "metric": "100", "ref": "0", "use": "0", - "iface": "ens33" + "iface": "ens33", + "mss": "0", + "window": "0", + "irtt": "0" }, { "destination": "172.17.0.0", @@ -26,7 +71,10 @@ $ route | jc --route -p "metric": "0", "ref": "0", "use": "0", - "iface": "docker0" + "iface": "docker", + "mss": "0", + "window": "0", + "irtt": "0" }, { "destination": "192.168.71.0", @@ -36,13 +84,47 @@ $ route | jc --route -p "metric": "100", "ref": "0", "use": "0", - "iface": "ens33" + "iface": "ens33", + "mss": "0", + "window": "0", + "irtt": "0" } ] """ 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): # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux', 'aix', 'freebsd'] @@ -56,4 +138,9 @@ def parse(data, raw=False, quiet=False): cleandata = data.splitlines()[1:] 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:]) - 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)