1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-21 00:19:42 +02:00

add iptables parser

This commit is contained in:
Kelly Brazil
2019-10-22 15:42:29 -07:00
parent 9ac5746996
commit 776ef2d1be
2 changed files with 61 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import jc.parsers.df
import jc.parsers.env import jc.parsers.env
import jc.parsers.free import jc.parsers.free
import jc.parsers.ifconfig import jc.parsers.ifconfig
import jc.parsers.iptables
import jc.parsers.ls import jc.parsers.ls
import jc.parsers.lsblk import jc.parsers.lsblk
import jc.parsers.mount import jc.parsers.mount
@ -38,6 +39,9 @@ def main():
elif '--ifconfig' in sys.argv: elif '--ifconfig' in sys.argv:
result = jc.parsers.ifconfig.parse(data) result = jc.parsers.ifconfig.parse(data)
elif '--iptables' in sys.argv:
result = jc.parsers.iptables.parse(data)
elif '--ls' in sys.argv: elif '--ls' in sys.argv:
result = jc.parsers.ls.parse(data) result = jc.parsers.ls.parse(data)
@ -67,6 +71,7 @@ def main():
print(' --env env parser', file=sys.stderr) print(' --env env parser', file=sys.stderr)
print(' --free free parser', file=sys.stderr) print(' --free free parser', file=sys.stderr)
print(' --ifconfig iconfig parser', file=sys.stderr) print(' --ifconfig iconfig parser', file=sys.stderr)
print(' --iptables iptables parser', file=sys.stderr)
print(' --ls ls parser', file=sys.stderr) print(' --ls ls parser', file=sys.stderr)
print(' --lsblk lsblk parser', file=sys.stderr) print(' --lsblk lsblk parser', file=sys.stderr)
print(' --mount mount parser', file=sys.stderr) print(' --mount mount parser', file=sys.stderr)

56
jc/parsers/iptables.py Normal file
View File

@ -0,0 +1,56 @@
"""jc - JSON CLI output utility ipables Parser
Usage:
Specify --iptables as the first argument if the piped input is coming from iptables
Supports -vLn for all tables
Examples:
"""
class state():
output = []
chain = {}
headers = []
def parse(data):
cleandata = data.splitlines()
for line in cleandata:
if line.find('Chain') == 0:
state.output.append(state.chain)
state.chain = {}
state.headers = []
state.chain['rules'] = []
parsed_line = line.split()
state.chain['chain'] = parsed_line[1]
state.chain['references'] = parsed_line[2].lstrip('(').rstrip(')').split()[0]
continue
if line.find('target') == 0:
state.headers = []
state.headers = [h for h in ' '.join(line.strip().split()).split() if h]
state.headers.append("options")
continue
else:
rule = line.split()
temp_rule = {}
for h, r in zip(state.headers, rule):
temp_rule[h] = r
state.chain['rules'].append(temp_rule)
continue
state.output = list(filter(None, state.output))
return state.output