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

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