42 lines
No EOL
1.2 KiB
Python
42 lines
No EOL
1.2 KiB
Python
import re
|
|
|
|
def parse_firewall_data(raw_json):
|
|
if not raw_json.get("success") or not raw_json.get("data"):
|
|
return []
|
|
|
|
text_data = raw_json["data"]
|
|
lines = text_data.strip().split('\n')
|
|
|
|
rulesets = []
|
|
current_ruleset = None
|
|
|
|
for line in lines:
|
|
header_match = re.search(r'Firewall "([^"]+)"', line)
|
|
if header_match:
|
|
ruleset_name = header_match.group(1)
|
|
current_ruleset = {
|
|
"name": ruleset_name,
|
|
"rules": []
|
|
}
|
|
rulesets.append(current_ruleset)
|
|
continue
|
|
|
|
if not current_ruleset or not line.strip() or line.startswith('---') or "Rule Action" in line:
|
|
continue
|
|
|
|
parts = line.split()
|
|
if len(parts) >= 5:
|
|
try:
|
|
rule_data = {
|
|
"rule": parts[0],
|
|
"action": parts[1],
|
|
"protocol": parts[2],
|
|
"packets": parts[3],
|
|
"bytes": parts[4],
|
|
"conditions": ' '.join(parts[5:])
|
|
}
|
|
current_ruleset["rules"].append(rule_data)
|
|
except IndexError:
|
|
continue
|
|
|
|
return rulesets |