From 1d31d3570add0bb813c1f95966c88e849168ffa2 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Mon, 18 Mar 2024 10:52:26 -0700 Subject: [PATCH] add -x support to iptables parser --- CHANGELOG | 3 ++- docs/parsers/iptables.md | 2 +- jc/parsers/iptables.py | 7 +++++-- tests/test_iptables.py | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index eaa2ea33..63f99298 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ jc changelog -20240315 v1.25.2 +20240318 v1.25.2 - Add `apt-cache-show` command parser - Add `apt-get-sqq` command parser - Add `ethtool` command parser @@ -10,6 +10,7 @@ jc changelog to bytes. - Enhance `free` parser to add human readable output support. All size values have been normalizd to bytes. +- Enhance `iptables` parser to handle -x output - Enhance `/proc/pid/stat` parser to support "Idle" state - Enhance `rpm_qi` and `pkg_index_deb` parsers to split list fields into arrays - Fix `iwconfig` parser to handle more special characters in the SSID name diff --git a/docs/parsers/iptables.md b/docs/parsers/iptables.md index 9639f2e3..8c44b32e 100644 --- a/docs/parsers/iptables.md +++ b/docs/parsers/iptables.md @@ -188,4 +188,4 @@ Compatibility: linux Source: [`jc/parsers/iptables.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/iptables.py) -Version 1.10 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.11 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/parsers/iptables.py b/jc/parsers/iptables.py index a2d52ea6..57560f18 100644 --- a/jc/parsers/iptables.py +++ b/jc/parsers/iptables.py @@ -163,7 +163,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.10' + version = '1.11' description = '`iptables` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -247,7 +247,10 @@ def parse(data, raw=False, quiet=False): continue - elif line.startswith('target') or line.find('pkts') == 1 or line.startswith('num'): + elif line.startswith('target') or \ + (line.find('pkts') >= 1 and line.find('pkts') <= 3) or \ + line.startswith('num'): + headers = [h for h in ' '.join(line.lower().strip().split()).split() if h] headers.append("options") diff --git a/tests/test_iptables.py b/tests/test_iptables.py index 26fd6752..edccb3ba 100644 --- a/tests/test_iptables.py +++ b/tests/test_iptables.py @@ -173,6 +173,20 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.iptables.parse(self.generic_iptables_no_jump, quiet=True), self.generic_iptables_no_jump_json) + def test_iptables_x_option_format(self): + """ + Test iptables -x + """ + data = '''Chain DOCKER-ISOLATION-STAGE-2 (4 references) + pkts bytes target prot opt in out source destination + 0 0 DROP all -- any docker0 anywhere anywhere + 0 0 DROP all -- any br-b01fa3a90d3b anywhere anywhere + 0 0 DROP all -- any br-642643a59593 anywhere anywhere + 0 0 DROP all -- any br-3e698d2f6bc4 anywhere anywhere +44758639 38517421321 RETURN all -- any any anywhere anywhere''' + expected = [{"chain":"DOCKER-ISOLATION-STAGE-2","rules":[{"pkts":0,"bytes":0,"target":"DROP","prot":"all","opt":None,"in":"any","out":"docker0","source":"anywhere","destination":"anywhere"},{"pkts":0,"bytes":0,"target":"DROP","prot":"all","opt":None,"in":"any","out":"br-b01fa3a90d3b","source":"anywhere","destination":"anywhere"},{"pkts":0,"bytes":0,"target":"DROP","prot":"all","opt":None,"in":"any","out":"br-642643a59593","source":"anywhere","destination":"anywhere"},{"pkts":0,"bytes":0,"target":"DROP","prot":"all","opt":None,"in":"any","out":"br-3e698d2f6bc4","source":"anywhere","destination":"anywhere"},{"pkts":44758639,"bytes":38517421321,"target":"RETURN","prot":"all","opt":None,"in":"any","out":"any","source":"anywhere","destination":"anywhere"}]}] + self.assertEqual(jc.parsers.iptables.parse(data, quiet=True), expected) + if __name__ == '__main__': unittest.main()