From 066adfb76479df7042bfb12bbb83b5dbd8a6d54c Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 22 Jul 2020 15:02:02 -0700 Subject: [PATCH] handle warning lines in the traceroute output --- jc/parsers/traceroute.py | 30 +++++++++++++---- tests/fixtures/centos-7.7/traceroute.out | 32 +++++++++++++++++++ tests/fixtures/osx-10.14.6/traceroute-asn.out | 5 +++ .../osx-10.14.6/traceroute-mult-addresses.out | 5 +++ .../osx-10.14.6/traceroute-no-header.out | 3 ++ .../osx-10.14.6/traceroute6-multi-address.out | 8 +++++ tests/fixtures/osx-10.14.6/traceroute6.out | 7 ++++ 7 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/centos-7.7/traceroute.out create mode 100644 tests/fixtures/osx-10.14.6/traceroute-asn.out create mode 100644 tests/fixtures/osx-10.14.6/traceroute-mult-addresses.out create mode 100644 tests/fixtures/osx-10.14.6/traceroute-no-header.out create mode 100644 tests/fixtures/osx-10.14.6/traceroute6-multi-address.out create mode 100644 tests/fixtures/osx-10.14.6/traceroute6.out diff --git a/jc/parsers/traceroute.py b/jc/parsers/traceroute.py index e55e924c..39db0b22 100644 --- a/jc/parsers/traceroute.py +++ b/jc/parsers/traceroute.py @@ -4,13 +4,16 @@ Usage: specify --traceroute as the first argument if the piped input is coming from traceroute + Note: on OSX and FreeBSD be sure to redirect STDERR to STDOUT since the header line is sent to STDERR + e.g. $ traceroute 8.8.8.8 2>&1 | jc --traceroute + Compatibility: 'linux', 'darwin', 'freebsd' Examples: - $ traceroute | jc --traceroute -p + $ traceroute www.cnn.com 2>&1 | jc --traceroute -p [] $ traceroute | jc --traceroute -p -r @@ -211,9 +214,7 @@ class ParseError(Exception): pass - - - +######################################################################################## def process(proc_data): """ @@ -261,6 +262,19 @@ def parse(data, raw=False, quiet=False): if jc.utils.has_data(data): + # remove any warning lines + new_data = [] + for data_line in data.splitlines(): + if 'traceroute: Warning: ' not in data_line and 'traceroute6: Warning: ' not in data_line: + new_data.append(data_line) + else: + continue + data = '\n'.join(new_data) + + # check if header row exists, otherwise raise exception + if not data.splitlines()[0].startswith('traceroute to ') and not data.splitlines()[0].startswith('traceroute6 to '): + raise ParseError('Traceroute header line not found. Be sure to redirect STDERR to STDOUT on OSX/FreeBSD.') + tr = loads(data) hops = tr.hops hops_list = [] @@ -268,18 +282,20 @@ def parse(data, raw=False, quiet=False): if hops: for hop in hops: hop_obj = {} - hop_obj['id'] = hop.idx + hop_obj['hop'] = str(hop.idx) probe_list = [] + if hop.probes: for probe in hop.probes: probe_obj = { 'annotation': probe.annotation, - 'asn': probe.asn, + 'asn': None if probe.asn is None else str(probe.asn), 'ip': probe.ip, 'name': probe.name, - 'rtt': None if probe.rtt is None else float(probe.rtt) + 'rtt': None if probe.rtt is None else str(probe.rtt) } probe_list.append(probe_obj) + hop_obj['probes'] = probe_list hops_list.append(hop_obj) diff --git a/tests/fixtures/centos-7.7/traceroute.out b/tests/fixtures/centos-7.7/traceroute.out new file mode 100644 index 00000000..87a379b9 --- /dev/null +++ b/tests/fixtures/centos-7.7/traceroute.out @@ -0,0 +1,32 @@ +traceroute to www.cnn.com (151.101.197.67), 30 hops max, 60 byte packets + 1 dsldevice.attlocal.net (192.168.1.254) 4.024 ms 3.683 ms 3.706 ms + 2 76-220-24-1.lightspeed.sntcca.sbcglobal.net (76.220.24.1) 24.038 ms 23.911 ms 23.724 ms + 3 * * * + 4 12.122.149.186 (12.122.149.186) 42.273 ms 42.213 ms 42.083 ms + 5 12.122.2.77 (12.122.2.77) 36.789 ms 43.691 ms 36.597 ms + 6 sd2ca21crs.ip.att.net (12.122.2.94) 41.587 ms 40.240 ms 44.659 ms + 7 12.123.215.161 (12.123.215.161) 37.858 ms 37.321 ms 37.092 ms + 8 * * * + 9 * * * +10 * * * +11 * * * +12 * * * +13 * * * +14 * * * +15 * * * +16 * * * +17 * * * +18 * * * +19 * * * +20 * * * +21 * * * +22 * * * +23 * * * +24 * * * +25 * * * +26 * * * +27 * * * +28 * * * +29 * * * +30 * * * + diff --git a/tests/fixtures/osx-10.14.6/traceroute-asn.out b/tests/fixtures/osx-10.14.6/traceroute-asn.out new file mode 100644 index 00000000..f7999273 --- /dev/null +++ b/tests/fixtures/osx-10.14.6/traceroute-asn.out @@ -0,0 +1,5 @@ +traceroute to 8.8.8.8 (8.8.8.8), 4 hops max, 52 byte packets + 1 [AS198949] dsldevice (192.168.1.254) 6.070 ms 5.721 ms 5.269 ms + 2 [AS0] 76-220-24-1.lightspeed.sntcca.sbcglobal.net (76.220.24.1) 160.025 ms 178.690 ms 33.759 ms + 3 * * * + 4 [AS7018] 12.122.149.186 (12.122.149.186) 37.783 ms 23.782 ms 24.958 ms diff --git a/tests/fixtures/osx-10.14.6/traceroute-mult-addresses.out b/tests/fixtures/osx-10.14.6/traceroute-mult-addresses.out new file mode 100644 index 00000000..d8bf4220 --- /dev/null +++ b/tests/fixtures/osx-10.14.6/traceroute-mult-addresses.out @@ -0,0 +1,5 @@ +traceroute: Warning: cnn.com has multiple addresses; using 151.101.129.67 +traceroute to cnn.com (151.101.129.67), 64 hops max, 52 byte packets + 1 dsldevice (192.168.1.254) 4.478 ms 3.907 ms 4.849 ms + 2 76-220-24-1.lightspeed.sntcca.sbcglobal.net (76.220.24.1) 23.530 ms 26.518 ms 23.480 ms + 3 * * \ No newline at end of file diff --git a/tests/fixtures/osx-10.14.6/traceroute-no-header.out b/tests/fixtures/osx-10.14.6/traceroute-no-header.out new file mode 100644 index 00000000..0c1932c3 --- /dev/null +++ b/tests/fixtures/osx-10.14.6/traceroute-no-header.out @@ -0,0 +1,3 @@ + 1 dsldevice (192.168.1.254) 11.415 ms 3.934 ms 3.286 ms + 2 76-220-24-1.lightspeed.sntcca.sbcglobal.net (76.220.24.1) 24.174 ms 20.817 ms 27.771 ms + 3 * * * diff --git a/tests/fixtures/osx-10.14.6/traceroute6-multi-address.out b/tests/fixtures/osx-10.14.6/traceroute6-multi-address.out new file mode 100644 index 00000000..e056c8ce --- /dev/null +++ b/tests/fixtures/osx-10.14.6/traceroute6-multi-address.out @@ -0,0 +1,8 @@ +traceroute6: Warning: turner-tls.map.fastly.net has multiple addresses; using 2a04:4e42:200::323 +traceroute6 to turner-tls.map.fastly.net (2a04:4e42:200::323) from 2600:1700:bab0:d40:985:f00a:98bd:f142, 5 hops max, 12 byte packets + 1 * * * + 2 2001:506:6000:11b:71:156:212:143 27.635 ms 20.383 ms 23.438 ms + 3 * * * + 4 2001:1890:ff:ff08:12:242:117:16 20.118 ms 20.327 ms 21.213 ms + 5 * * * + diff --git a/tests/fixtures/osx-10.14.6/traceroute6.out b/tests/fixtures/osx-10.14.6/traceroute6.out new file mode 100644 index 00000000..8af70abd --- /dev/null +++ b/tests/fixtures/osx-10.14.6/traceroute6.out @@ -0,0 +1,7 @@ +traceroute6 to turner-tls.map.fastly.net (2a04:4e42:200::323) from 2600:1700:bab0:d40:985:f00a:98bd:f142, 5 hops max, 12 byte packets + 1 * * * + 2 2001:506:6000:11b:71:156:212:143 27.635 ms 20.383 ms 23.438 ms + 3 * * * + 4 2001:1890:ff:ff08:12:242:117:16 20.118 ms 20.327 ms 21.213 ms + 5 * * * +