I have a text file that looks like this
127.0.0.1
159.187.32.13, 3:00:15, flags: S
Incoming interface: Ethernet51/1
RPF route: [U] 151.177.45.0/27 [20/0] via 190.150.1.2
Outgoing interface list:
Vlan4054
159.187.32.20, 2:20:11, flags: S
Incoming interface: Ethernet51/1
RPF route: [U] 151.177.45.59/27 [20/0] via 190.150.1.2
Outgoing interface list:
Vlan4054
Vlan4056
198.140.45.77, 2:36:15, flags: S
Incoming interface: Ethernet51/1
RPF route: [U] 151.177.45.88/27 [20/0] via 190.150.1.2
Outgoing interface list:
Vlan4054
127.0.0.2
188.125.45.13, 3:00:15, flags: S
Incoming interface: Ethernet51/1
RPF route: [U] 199.150.45.0/27 [20/0] via 195.32.1.2
Outgoing interface list:
Vlan4054
Vlan4056
221.125.45.77, 2:20:11, flags: S
Incoming interface: Ethernet51/1
RPF route: [U] 199.150.45.10/27 [20/0] via 195.32.1.2
Outgoing interface list:
Vlan4054
Vlan4056
I'm trying to create a dictionary of the data so it is parseable, currently attempting to do so via regex
import re
content = []
content_dict = {}
group_ip = re.compile("^(\d+\.\d+\.\d+\.\d+$)")
ip_subnet = re.compile("^(\d+\.\d+\.\d+\.\d+\/+\d+)")
two_space_start = re.compile("^( {2})\S")
four_space_start = re.compile("^( {4})\S")
six_space_start = re.compile("^( {6})\S")
I had planned on applying regex to each line and creating a dictionary like below
if group_ip.match(line):
content_dict["group"] = line.strip()
elif two_space.match(line) and "RP" in line:
line = line.split(",")
content_dict["source"] = line[0].strip()
content_dict["uptime"] = line[1].strip()
content_dict["rp"] = line[2].split(" ")[-1]
content_dict["source_flags"] = line[-1].split(":")[-1].strip()
content.append(copy.copy(content_dict))
But have realised that this won't work on scale as each group IP (127.0.0.1, 127.0.0.2) will have a variable amount of subgroups that I am overwriting. What I'm trying to get to is something along the lines of
"127.0.0.1": [
"159.187.32.13": [
"uptime": "3:00:15",
"flags": "S",
"rpf_ip": "151.177.45.0/27",
"via": "190.150.1.2",
"outgoing_interface": ["vlan4054"]
],
"159.187.32.20": [
"uptime": "2:20:11",
"flags": "S",
"rpf_ip": "151.177.45.59/27",
"via": "190.150.1.2",
"outgoing_interface": ["Vlan4054", "Vlan4056"]
]
]
Is it possible to get this data structure from the text through regex or some other way?
"127.0.0.1","127.0.0.2"). This would be difficult to use this structured data.