From ac831444cea3232f2a36ce954a81ea3650fef7ad Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 11:40:53 -0700 Subject: [PATCH 1/6] fix for older python versions that don't handle decimal ip's cleanly --- jc/parsers/ip_address.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/jc/parsers/ip_address.py b/jc/parsers/ip_address.py index e470d08e..7789e552 100644 --- a/jc/parsers/ip_address.py +++ b/jc/parsers/ip_address.py @@ -468,7 +468,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = 'IPv4 and IPv6 Address string parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -565,7 +565,12 @@ def parse( broadcast_string = str(network.broadcast_address) broadcast_ipobj = ipaddress.ip_address(broadcast_string) - hostmask_string = str(interface.with_hostmask).split('/')[1] + # older versions of python (e.g. 3.6) don't provide hostmask when a decimal IP is entered + try: + hostmask_string = str(interface.hostmask) + except AttributeError: + hostmask_string = '0.0.0.0' + hostmask_ipobj = ipaddress.ip_address(hostmask_string) netmask_string = str(interface.with_netmask).split('/')[1] From 980fc77812e9c151da6632d416bf7f975e2eef92 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 11:49:32 -0700 Subject: [PATCH 2/6] version bump --- CHANGELOG | 4 ++++ docs/parsers/ip_address.md | 2 +- jc/lib.py | 2 +- man/jc.1 | 2 +- setup.py | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 06ae27f2..1b0c2c21 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ jc changelog +xxxxxxxx v1.21.1 +- Fix IP Address string parser for older python versions that don't cleanly + accept decimal input format (e.g. python 3.6) + 20220821 v1.21.0 - Add IP Address string parser - Add Syslog standard and streaming string parsers (RFC 3164 and RFC 5424) diff --git a/docs/parsers/ip_address.md b/docs/parsers/ip_address.md index aa664e73..909f17bd 100644 --- a/docs/parsers/ip_address.md +++ b/docs/parsers/ip_address.md @@ -487,4 +487,4 @@ Returns: ### Parser Information Compatibility: linux, darwin, cygwin, win32, aix, freebsd -Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index 9356323d..b4698536 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -6,7 +6,7 @@ import importlib from typing import Dict, List, Iterable, Union, Iterator from jc import appdirs -__version__ = '1.21.0' +__version__ = '1.21.1' parsers = [ 'acpi', diff --git a/man/jc.1 b/man/jc.1 index 8febd814..7b3d3da3 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-08-24 1.21.0 "JSON Convert" +.TH jc 1 2022-08-28 1.21.1 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools and file-types .SH SYNOPSIS diff --git a/setup.py b/setup.py index d92354ab..8634cf8a 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.21.0', + version='1.21.1', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='Converts the output of popular command-line tools and file-types to JSON.', From c7fc2e3b92fd2ad317bb8c8738959658695454f4 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 11:58:51 -0700 Subject: [PATCH 3/6] fix for older python versions that don't provide the netmask attribute when a decimal ip is used --- jc/parsers/ip_address.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jc/parsers/ip_address.py b/jc/parsers/ip_address.py index 7789e552..1b807dde 100644 --- a/jc/parsers/ip_address.py +++ b/jc/parsers/ip_address.py @@ -573,7 +573,12 @@ def parse( hostmask_ipobj = ipaddress.ip_address(hostmask_string) - netmask_string = str(interface.with_netmask).split('/')[1] + # older versions of python (e.g. 3.6) don't provide netmask when a decimal IP is entered + try: + netmask_string = str(interface.netmask) + except AttributeError: + netmask_string = '255.255.255.255' + netmask_ipobj = ipaddress.ip_address(netmask_string) bare_ip_string = str(interface.ip) From fc06d195ec2e73825feb4ca4380d272568508a36 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 12:31:57 -0700 Subject: [PATCH 4/6] fix for arp -a cases where there are incomplete hw addresses --- jc/parsers/arp.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index e06003fd..e1e24f0c 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -119,7 +119,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.10' + version = '1.11' description = '`arp` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -221,13 +221,24 @@ def parse( else: for line in cleandata: splitline = line.split() - output_line = { - 'name': splitline[0], - 'address': splitline[1].lstrip('(').rstrip(')'), - 'hwtype': splitline[4].lstrip('[').rstrip(']'), - 'hwaddress': splitline[3], - 'iface': splitline[6], - } + if '' not in splitline: + output_line = { + 'name': splitline[0], + 'address': splitline[1].lstrip('(').rstrip(')'), + 'hwtype': splitline[4].lstrip('[').rstrip(']'), + 'hwaddress': splitline[3], + 'iface': splitline[6], + } + + else: + output_line = { + 'name': splitline[0], + 'address': splitline[1].lstrip('(').rstrip(')'), + 'hwtype': None, + 'hwaddress': None, + 'iface': splitline[5], + } + raw_output.append(output_line) return raw_output if raw else _process(raw_output) From 4d4b95c995ef31eace76dddff2f1bb96b60548bf Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 12:40:46 -0700 Subject: [PATCH 5/6] fix for linx arp -a cases where an icomplete hw address is present --- CHANGELOG | 2 ++ docs/parsers/arp.md | 2 +- tests/fixtures/centos-8/arp-a.json | 1 + tests/fixtures/centos-8/arp-a.out | 4 ++++ tests/test_arp.py | 12 ++++++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/centos-8/arp-a.json create mode 100644 tests/fixtures/centos-8/arp-a.out diff --git a/CHANGELOG b/CHANGELOG index 1b0c2c21..0c4566f7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,8 @@ jc changelog xxxxxxxx v1.21.1 - Fix IP Address string parser for older python versions that don't cleanly accept decimal input format (e.g. python 3.6) +- Fix `arp -a` parser for cases where incomplete hardware addresses are found + in the arp table on linux 20220821 v1.21.0 - Add IP Address string parser diff --git a/docs/parsers/arp.md b/docs/parsers/arp.md index 59750bf3..294128c7 100644 --- a/docs/parsers/arp.md +++ b/docs/parsers/arp.md @@ -140,4 +140,4 @@ Returns: ### Parser Information Compatibility: linux, aix, freebsd, darwin -Version 1.10 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.11 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/tests/fixtures/centos-8/arp-a.json b/tests/fixtures/centos-8/arp-a.json new file mode 100644 index 00000000..c85f2008 --- /dev/null +++ b/tests/fixtures/centos-8/arp-a.json @@ -0,0 +1 @@ +[{"name":null,"address":"192.168.71.21","hwtype":null,"hwaddress":null,"iface":"ens33"},{"name":null,"address":"192.168.71.128","hwtype":null,"hwaddress":null,"iface":"ens33"},{"name":null,"address":"192.168.71.254","hwtype":"ether","hwaddress":"00:50:56:e2:91:0e","iface":"ens33"},{"name":null,"address":"192.168.71.226","hwtype":null,"hwaddress":null,"iface":"ens33"}] diff --git a/tests/fixtures/centos-8/arp-a.out b/tests/fixtures/centos-8/arp-a.out new file mode 100644 index 00000000..a2c47922 --- /dev/null +++ b/tests/fixtures/centos-8/arp-a.out @@ -0,0 +1,4 @@ +? (192.168.71.21) at on ens33 +? (192.168.71.128) at on ens33 +? (192.168.71.254) at 00:50:56:e2:91:0e [ether] on ens33 +? (192.168.71.226) at on ens33 diff --git a/tests/test_arp.py b/tests/test_arp.py index 0d0e6850..5ac7a6c3 100644 --- a/tests/test_arp.py +++ b/tests/test_arp.py @@ -40,6 +40,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/arp-a.out'), 'r', encoding='utf-8') as f: self.freebsd_arp_a = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/arp-a.out'), 'r', encoding='utf-8') as f: + self.centos8_arp_a = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/arp.json'), 'r', encoding='utf-8') as f: self.centos_7_7_arp_json = json.loads(f.read()) @@ -71,6 +74,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/arp-a.json'), 'r', encoding='utf-8') as f: self.freebsd12_arp_a_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/arp-a.json'), 'r', encoding='utf-8') as f: + self.centos8_arp_a_json = json.loads(f.read()) + def test_arp_nodata(self): """ Test 'arp' with no data @@ -137,6 +143,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.arp.parse(self.freebsd_arp_a, quiet=True), self.freebsd12_arp_a_json) + def test_arp_a_centos8(self): + """ + Test 'arp -a' on CentOS 8 with incomplete hw addresses + """ + self.assertEqual(jc.parsers.arp.parse(self.centos8_arp_a, quiet=True), self.centos8_arp_a_json) + if __name__ == '__main__': unittest.main() From 0b726f7accf819c69c18d582682f11494c321cb1 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 28 Aug 2022 16:41:06 -0700 Subject: [PATCH 6/6] doc update --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 0c4566f7..4ef7f8dc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ jc changelog -xxxxxxxx v1.21.1 +20220828 v1.21.1 - Fix IP Address string parser for older python versions that don't cleanly accept decimal input format (e.g. python 3.6) - Fix `arp -a` parser for cases where incomplete hardware addresses are found