Commit cba8b3ee authored by Simon Redman's avatar Simon Redman

Add helper to push ULA ipv6 addresses to the network

parent ca43c3e1
......@@ -18,6 +18,7 @@
import ssh_helper
import argparse
import datetime
import getpass
import ipaddress
from netdiff import NetJsonParser
......@@ -26,6 +27,11 @@ import re
LINK_LOCAL_COMMAND = "ip -6 address list"
IPV4_COMMAND = "ip -4 address list"
ULA_ADDER_HEADER_TEMPLATE = "#!/usr/bin/env bash\n# Script generated by at {datetime}"
ADD_ADDR_COMMAND_TEMPLATE = "sudo ip -6 addr add {addr} dev {dev}"
def parse_ipv4_networks(netgraph):
......@@ -132,19 +138,45 @@ def convert_link_local_addr_to_ULA(link_local_interface, ipv4_interface):
return site_local_intf
def add_ULAs_to_hosts(netgraph, ULA_map):
Generate per-host scripts which add the Unique Local Addresses, copy them to the hosts, and execute them
Leave the scripts in place for debugging purposes
:param netgraph: networkx graph representing the network, annotated with logged-in ssh sessions
:param ULA_map: mapping of hosts to interfaces to ULA addresses
:return: None
hostnames = [host for host in netgraph.nodes]
sessions = [] # Ordered list of logged-in ssh sessions, one per host
scripts = [] # Ordered list of series of commands to run on each host
for host_idx in range(len(hostnames)):
host = hostnames[host_idx]
for interface in ULA_map[host].keys():
addr = ULA_map[host][interface]
command = ADD_ADDR_COMMAND_TEMPLATE.format(addr=addr, dev=interface)
scripts[host_idx] += "\n" + command
# Push the scripts to the remotes and execute them
commands = []
for host_idx in range(len(hostnames)):
script = scripts[host_idx].format(
ssh_helper.run_commands_on_many_hosts(sessions, commands)
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 the annotated NetJSON. Default 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 or args.in_file
netgraph = NetJsonParser(file=args.in_file)
hostnames = [node for node in netgraph.graph.nodes]
usernames = [args.username for i in range(len(hostnames))]
......@@ -161,4 +193,10 @@ if __name__ == "__main__":
ipv4_addr = ipv4_map[host][interface]
ULA_map[host][interface] = convert_link_local_addr_to_ULA(link_local_addr, ipv4_addr)
add_ULAs_to_hosts(netgraph.graph, ULA_map)
for node in netgraph.graph._node:
session = netgraph.graph._node[node]['session']
del netgraph.graph._node[node]['session']
\ No newline at end of file
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