1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-15 01:24:29 +02:00

add -x support to iptables parser

This commit is contained in:
Kelly Brazil
2024-03-18 10:52:26 -07:00
parent b84123a09b
commit 1d31d3570a
4 changed files with 22 additions and 4 deletions

View File

@ -1,6 +1,6 @@
jc changelog jc changelog
20240315 v1.25.2 20240318 v1.25.2
- Add `apt-cache-show` command parser - Add `apt-cache-show` command parser
- Add `apt-get-sqq` command parser - Add `apt-get-sqq` command parser
- Add `ethtool` command parser - Add `ethtool` command parser
@ -10,6 +10,7 @@ jc changelog
to bytes. to bytes.
- Enhance `free` parser to add human readable output support. All size values - Enhance `free` parser to add human readable output support. All size values
have been normalizd to bytes. have been normalizd to bytes.
- Enhance `iptables` parser to handle -x output
- Enhance `/proc/pid/stat` parser to support "Idle" state - Enhance `/proc/pid/stat` parser to support "Idle" state
- Enhance `rpm_qi` and `pkg_index_deb` parsers to split list fields into arrays - 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 - Fix `iwconfig` parser to handle more special characters in the SSID name

View File

@ -188,4 +188,4 @@ Compatibility: linux
Source: [`jc/parsers/iptables.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/iptables.py) 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)

View File

@ -163,7 +163,7 @@ import jc.utils
class info(): class info():
"""Provides parser metadata (version, author, etc.)""" """Provides parser metadata (version, author, etc.)"""
version = '1.10' version = '1.11'
description = '`iptables` command parser' description = '`iptables` command parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
@ -247,7 +247,10 @@ def parse(data, raw=False, quiet=False):
continue 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 = [h for h in ' '.join(line.lower().strip().split()).split() if h]
headers.append("options") headers.append("options")

View File

@ -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) 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__': if __name__ == '__main__':
unittest.main() unittest.main()