Creating initial files

This commit is contained in:
Dom 2023-11-28 22:50:12 +00:00
commit dc7e9de16e
5 changed files with 135 additions and 0 deletions

8
main.py Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env python3
from topology.topo import Topology
if __name__ == '__main__':
topology = Topology()
topology.render()
topology.get_dns_records()

10
records.txt Normal file
View File

@ -0,0 +1,10 @@
10.0.0.1 LAB-RELAY_GigabitEthernet0-->ge-0/0/0_LAB-SRX300
10.0.0.0 LAB-SRX300_ge-0/0/0-->GigabitEthernet0_LAB-RELAY
10.0.0.3 LAB-RELAY_GigabitEthernet1-->GigabitEthernet8_LAB-897VA
10.0.0.2 LAB-897VA_GigabitEthernet8-->GigabitEthernet1_LAB-RELAY
192.168.0.249 LAB-RELAY_eth0-->GigabitEthernet8_UBUNTU
192.168.0.247 UBUNTU_GigabitEthernet8-->eth0_LAB-RELAY
10.0.0.4 LAB-SRX300_GigabitEthernet1-->ge-0/0/1_LAB-897VA
10.0.0.5 LAB-897VA_ge-0/0/1-->GigabitEthernet1_LAB-SRX300
10.0.0.6 LAB-SRX300_GigabitEthernet2-->ge-0/0/2_LAB-897VA
10.0.0.7 LAB-897VA_ge-0/0/2-->GigabitEthernet2_LAB-SRX300

47
topology.yaml Normal file
View File

@ -0,0 +1,47 @@
Lab:
nodes:
UBUNTU
LAB-RELAY
LAB-897VA
LAB-SRX300
edges:
1:
a_end: LAB-RELAY
b_end: LAB-SRX300
a_end_ip: 10.0.0.0
b_end_ip: 10.0.0.1
a_end_interface: GigabitEthernet0
b_end_interface: ge-0/0/0
edge_index: 0
2:
a_end: LAB-RELAY
b_end: LAB-897VA
a_end_ip: 10.0.0.2
b_end_ip: 10.0.0.3
a_end_interface: GigabitEthernet1
b_end_interface: GigabitEthernet8
edge_index: 0
3:
a_end: UBUNTU
b_end: LAB-RELAY
a_end_ip: 192.168.0.247
b_end_ip: 192.168.0.249
a_end_interface: eth0
b_end_interface: GigabitEthernet8
edge_index: 0
4:
a_end: LAB-897VA
b_end: LAB-SRX300
a_end_ip: 10.0.0.5
b_end_ip: 10.0.0.4
a_end_interface: GigabitEthernet1
b_end_interface: ge-0/0/1
edge_index: 0
5:
a_end: LAB-897VA
b_end: LAB-SRX300
a_end_ip: 10.0.0.7
b_end_ip: 10.0.0.6
a_end_interface: GigabitEthernet2
b_end_interface: ge-0/0/2
edge_index: 1

70
topology/topo.py Normal file
View File

@ -0,0 +1,70 @@
import networkx as nx
import yaml
from networkx.drawing.nx_agraph import to_agraph
class Topology(object):
def __init__(self):
self.G = nx.MultiGraph()
def render(self):
'''
Summary:
Builds a network topology from the topology.yaml file into memory, stored in a networkx Graph object.
Once the networkx Graph object has been built, it's drawn and saved as topology.png.
Nodes: network devices
Edges: links between network devices
Edge attributes: dict of attributes assigned to a specific edge
'''
topology_data = yaml.load(open('topology.yaml'), Loader=yaml.SafeLoader)
for topology_name, topology_vars in topology_data.items():
for node in topology_vars['nodes']:
self.G.add_node(node)
self.G.nodes[node]['shape'] = 'box'
for edge_number, edge_attributes in topology_vars['edges'].items():
self.G.add_edge(edge_attributes['a_end'], edge_attributes['b_end'])
nx.set_edge_attributes(
self.G,
{
(edge_attributes['a_end'],
edge_attributes['b_end'],
edge_attributes['edge_index']): edge_attributes
}
)
for node1, node2, edge_attributes in self.G.edges(data=True):
edge_attributes['label'] = f"""
{edge_attributes['a_end']}
{edge_attributes['a_end_ip']}, {edge_attributes['a_end_interface']}\n
{edge_attributes['b_end']}
{edge_attributes['b_end_ip']}, {edge_attributes['b_end_interface']}
"""
vis = to_agraph(self.G)
vis.layout('dot')
vis_path = 'topology/topology.png'
vis.draw(vis_path)
def get_dns_records(self):
'''
Summary:
Creates & outputs Ubuntu DNS entries for each edge IP in the networkx Graph object.
Enabling operators to easily visualise the path traffic has taken across a network.
dom@upsrv01:~/Python/T2DNS$ traceroute 10.0.0.3 --resolve -I
traceroute to 10.0.0.3 (10.0.0.3), 64 hops max
1 192.168.0.249 (UBUNTU_eth0-->GigabitEthernet8_LAB-RELAY) 0.864ms 0.688ms 0.720ms
2 10.0.0.3 (LAB-RELAY_GigabitEthernet1-->GigabitEthernet8_LAB-897VA) 1.431ms 1.372ms 1.363ms
'''
records = {}
print('Creating DNS records...')
for node1, node2, edge_attributes in self.G.edges(data=True):
records.update({edge_attributes['b_end_ip']: f"{node1}_{edge_attributes['a_end_interface']}-->{edge_attributes['b_end_interface']}_{node2}"})
records.update({edge_attributes['a_end_ip']: f"{node2}_{edge_attributes['b_end_interface']}-->{edge_attributes['a_end_interface']}_{node1}"})
for ip, record in records.items():
print(f'{ip} {record}')

BIN
topology/topology.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB