diff --git a/docs/parsers/proc_net_arp.md b/docs/parsers/proc_net_arp.md new file mode 100644 index 00000000..0585b5ca --- /dev/null +++ b/docs/parsers/proc_net_arp.md @@ -0,0 +1,81 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_arp + +jc - JSON Convert `/proc/net/arp` file parser + +Usage (cli): + + $ cat /proc/net/arp | jc --proc + +or + + $ jc /proc/net/arp + +or + + $ cat /proc/net/arp | jc --proc-net-arp + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_arp_file) + +or + + import jc + result = jc.parse('proc_net_arp', proc_net_arp_file) + +Schema: + + [ + { + "IP_address": string, + "HW_type": string, + "Flags": string, + "HW_address": string, + "Mask": string, + "Device": string + } + ] + +Examples: + + $ cat /proc/net/arp | jc --proc -p + [ + { + "IP_address": "192.168.71.254", + "HW_type": "0x1", + "Flags": "0x2", + "HW_address": "00:50:56:f3:2f:ae", + "Mask": "*", + "Device": "ens33" + }, + ... + ] + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> List[Dict] +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + +Returns: + + List of Dictionaries. Raw or processed structured data. + +### Parser Information +Compatibility: linux + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index c26e4ad2..e156054e 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -121,6 +121,7 @@ parsers = [ 'proc-pid-stat', 'proc-pid-statm', 'proc-pid-status', + 'proc-net-arp', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_arp.py b/jc/parsers/proc_net_arp.py new file mode 100644 index 00000000..1535326d --- /dev/null +++ b/jc/parsers/proc_net_arp.py @@ -0,0 +1,116 @@ +"""jc - JSON Convert `/proc/net/arp` file parser + +Usage (cli): + + $ cat /proc/net/arp | jc --proc + +or + + $ jc /proc/net/arp + +or + + $ cat /proc/net/arp | jc --proc-net-arp + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_arp_file) + +or + + import jc + result = jc.parse('proc_net_arp', proc_net_arp_file) + +Schema: + + [ + { + "IP_address": string, + "HW_type": string, + "Flags": string, + "HW_address": string, + "Mask": string, + "Device": string + } + ] + +Examples: + + $ cat /proc/net/arp | jc --proc -p + [ + { + "IP_address": "192.168.71.254", + "HW_type": "0x1", + "Flags": "0x2", + "HW_address": "00:50:56:f3:2f:ae", + "Mask": "*", + "Device": "ens33" + }, + ... + ] +""" +from typing import List, Dict +import jc.utils +from jc.parsers.universal import simple_table_parse + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/net/arp` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__version__ = info.version + + +def _process(proc_data: List[Dict]) -> List[Dict]: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (List of Dictionaries) raw structured data to process + + Returns: + + List of Dictionaries. Structured to conform to the schema. + """ + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> List[Dict]: + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + List of Dictionaries. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: List = [] + + if jc.utils.has_data(data): + + header = 'IP_address HW_type Flags HW_address Mask Device' + data_splitlines = data.splitlines() + data_splitlines[0] = header + raw_output = simple_table_parse(data_splitlines) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 049b2b4e..bd18b318 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -590,6 +590,11 @@ PLIST file parser \fB--proc-pid-status\fP `/proc//status` file parser +.TP +.B +\fB--proc-net-arp\fP +`/proc/net/arp` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/net_arp.json b/tests/fixtures/linux-proc/net_arp.json new file mode 100644 index 00000000..e961a02d --- /dev/null +++ b/tests/fixtures/linux-proc/net_arp.json @@ -0,0 +1 @@ +[{"IP_address":"192.168.71.254","HW_type":"0x1","Flags":"0x2","HW_address":"00:50:56:f3:2f:ae","Mask":"*","Device":"ens33"},{"IP_address":"192.168.71.2","HW_type":"0x1","Flags":"0x2","HW_address":"00:50:56:f7:4a:fc","Mask":"*","Device":"ens33"},{"IP_address":"192.168.71.1","HW_type":"0x1","Flags":"0x2","HW_address":"a6:83:e7:d2:a9:65","Mask":"*","Device":"ens33"}] diff --git a/tests/test_proc_net_arp.py b/tests/test_proc_net_arp.py new file mode 100644 index 00000000..3d1acbf0 --- /dev/null +++ b/tests/test_proc_net_arp.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_arp + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + @classmethod + def setUpClass(cls): + fixtures = { + 'proc_net_arp': ( + 'fixtures/linux-proc/net_arp', + 'fixtures/linux-proc/net_arp.json') + } + + for file, filepaths in fixtures.items(): + with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \ + open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b: + cls.f_in[file] = a.read() + cls.f_json[file] = json.loads(b.read()) + + + def test_proc_net_arp_nodata(self): + """ + Test 'proc_net_arp' with no data + """ + self.assertEqual(jc.parsers.proc_net_arp.parse('', quiet=True), []) + + def test_proc_net_arp(self): + """ + Test '/proc/net/arp' + """ + self.assertEqual(jc.parsers.proc_net_arp.parse(self.f_in['proc_net_arp'], quiet=True), + self.f_json['proc_net_arp']) + + +if __name__ == '__main__': + unittest.main()