From 3ca8e1ef11bc011e726009f3e7db82869fbbd6b1 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 19 Mar 2024 14:38:20 -0700 Subject: [PATCH] iptables -x support for larger numbers and doc update --- docs/parsers/iptables.md | 2 +- jc/parsers/iptables.py | 4 ++-- man/jc.1 | 2 +- tests/test_iptables.py | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/parsers/iptables.md b/docs/parsers/iptables.md index 8c44b32e..8c41a330 100644 --- a/docs/parsers/iptables.md +++ b/docs/parsers/iptables.md @@ -5,7 +5,7 @@ jc - JSON Convert `iptables` command output parser -Supports `-vLn` and `--line-numbers` for all tables. +Supports `-vLnx` and `--line-numbers` for all tables. Usage (cli): diff --git a/jc/parsers/iptables.py b/jc/parsers/iptables.py index 57560f18..dcaab5eb 100644 --- a/jc/parsers/iptables.py +++ b/jc/parsers/iptables.py @@ -1,6 +1,6 @@ r"""jc - JSON Convert `iptables` command output parser -Supports `-vLn` and `--line-numbers` for all tables. +Supports `-vLnx` and `--line-numbers` for all tables. Usage (cli): @@ -248,7 +248,7 @@ def parse(data, raw=False, quiet=False): continue elif line.startswith('target') or \ - (line.find('pkts') >= 1 and line.find('pkts') <= 3) or \ + (line.find('pkts') >= 1 and line.find('pkts') <= 5) or \ line.startswith('num'): headers = [h for h in ' '.join(line.lower().strip().split()).split() if h] diff --git a/man/jc.1 b/man/jc.1 index 2ffa0c89..604a895f 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2024-03-18 1.25.2 "JSON Convert" +.TH jc 1 2024-03-19 1.25.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings diff --git a/tests/test_iptables.py b/tests/test_iptables.py index edccb3ba..124a54e0 100644 --- a/tests/test_iptables.py +++ b/tests/test_iptables.py @@ -187,6 +187,22 @@ class MyTests(unittest.TestCase): 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) + def test_iptables_x_option_format2(self): + """ + Test iptables -x + """ + data = '''Chain INPUT (policy ACCEPT 0 packets, 0 bytes) + pkts bytes target prot opt in out source destination +11291792498 217331852907122 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED + 555958 33533576 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 +128628404869 172804745659762 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 +128627559128 172804718596050 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 +128627559125 172804718595966 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 + 26599 1082920 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID + 1761 79571 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited''' + expected = [{"chain":"INPUT","rules":[{"pkts":11291792498,"bytes":217331852907122,"target":"ACCEPT","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0","options":"ctstate RELATED,ESTABLISHED"},{"pkts":555958,"bytes":33533576,"target":"ACCEPT","prot":"all","opt":None,"in":"lo","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0"},{"pkts":128628404869,"bytes":172804745659762,"target":"INPUT_direct","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0"},{"pkts":128627559128,"bytes":172804718596050,"target":"INPUT_ZONES_SOURCE","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0"},{"pkts":128627559125,"bytes":172804718595966,"target":"INPUT_ZONES","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0"},{"pkts":26599,"bytes":1082920,"target":"DROP","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0","options":"ctstate INVALID"},{"pkts":1761,"bytes":79571,"target":"REJECT","prot":"all","opt":None,"in":"*","out":"*","source":"0.0.0.0/0","destination":"0.0.0.0/0","options":"reject-with icmp-host-prohibited"}]}] + self.assertEqual(jc.parsers.iptables.parse(data, quiet=True), expected) + if __name__ == '__main__': unittest.main()