Commit 40f1b43c authored by Simon Redman's avatar Simon Redman

Add support for writing interfaces and routable IPv6 addresses back to the NetGraph

parent 55fdc666
......@@ -42,6 +42,8 @@ The suggested usage is:
- Use the annotated graph to call parse_local_interface_addresses and parse_ipv4_networks
- Use the results from the previous calls to call construct_ULAs
- Use the result of construct_ULAs and the network graph to call add_ULAs_to_hosts
- Use the result of construct_ULAs to call add_interfaces_to_netgraph to add the interfaces and the associated IPv6 addresses to the NetGraph
- This adds an `interfaces` property to the nodes in the graph which maps the (Linux) interface name to the ULA IPv6 address
- Use ssh_helper to log out of each host in the network
## TODO
......
......@@ -21,6 +21,7 @@ import argparse
import datetime
import getpass
import ipaddress
import json
from netdiff import NetJsonParser
import re
......@@ -209,15 +210,35 @@ def add_ULAs_to_hosts(netgraph, ULA_map):
ssh_helper.run_commands_on_many_hosts(sessions, commands)
def add_interfaces_to_netgraph(netgraph, ULA_map):
"""
Add the routable IPv6 interfaces to the netgraph as the "interfaces" property
:param netgraph: networkx graph object to annotate
:param ULA_map: mapping of hosts to interfaces to ULA addresses
:return: None
"""
for host in netgraph.nodes:
node = netgraph._node[host]
interfaces = ULA_map[host]
node['interfaces'] = {}
for interface in interfaces:
node['interfaces'][interface] = str(interfaces[interface])
if __name__ == "__main__":
parser = argparse.ArgumentParser("Generate IPv6 Unique Local Addresses for all nodes in the network")
parser.add_argument("--in-file", action='store', type=str, required=True,
help="Path to the NetJSON file to parse")
parser.add_argument("--out-file", action='store', type=str, required=False,
help="Path to write updated NetJSON NetGraph. Defaults to overwriting in-file")
parser.add_argument("--username", action='store', type=str, default=getpass.getuser(),
help="Username to use on all hosts. Defaults to current user's username")
args = parser.parse_args()
args.out_file = args.out_file if args.out_file else args.in_file
netgraph = NetJsonParser(file=args.in_file)
ssh_helper.network_graph_login(netgraph.graph, args.username)
management_ips = [node["management-ip"] for node in netgraph.graph._node.values()]
......@@ -227,6 +248,10 @@ if __name__ == "__main__":
ULA_map = construct_ULAs(link_local_map, ipv4_map, management_ips)
add_ULAs_to_hosts(netgraph.graph, ULA_map)
add_interfaces_to_netgraph(netgraph.graph, ULA_map)
ssh_helper.network_graph_logout(netgraph.graph)
netjson = netgraph.json(dict=True)
with open(args.out_file, 'w') as outfile:
json.dump(netjson, outfile)
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment