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:
5
jc/jc.py
5
jc/jc.py
@ -10,6 +10,7 @@ import jc.parsers.df
|
||||
import jc.parsers.env
|
||||
import jc.parsers.free
|
||||
import jc.parsers.ifconfig
|
||||
import jc.parsers.iptables
|
||||
import jc.parsers.ls
|
||||
import jc.parsers.lsblk
|
||||
import jc.parsers.mount
|
||||
@ -38,6 +39,9 @@ def main():
|
||||
elif '--ifconfig' in sys.argv:
|
||||
result = jc.parsers.ifconfig.parse(data)
|
||||
|
||||
elif '--iptables' in sys.argv:
|
||||
result = jc.parsers.iptables.parse(data)
|
||||
|
||||
elif '--ls' in sys.argv:
|
||||
result = jc.parsers.ls.parse(data)
|
||||
|
||||
@ -67,6 +71,7 @@ def main():
|
||||
print(' --env env parser', file=sys.stderr)
|
||||
print(' --free free 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(' --lsblk lsblk parser', file=sys.stderr)
|
||||
print(' --mount mount parser', file=sys.stderr)
|
||||
|
56
jc/parsers/iptables.py
Normal file
56
jc/parsers/iptables.py
Normal 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
|
Reference in New Issue
Block a user