def collect_sh_ip_route(anm, nidb, start_node=None): if not start_node: start_node = random.choice([n for n in nidb.nodes("is_router")]) #TODO: move this to another module, eg measure import autonetkit.measure as measure import autonetkit.verify as verify import autonetkit command = 'vtysh -c "show ip route"' #TODO: make auto take tap ip if netkit platform node #TODO: auto make put into list if isinstance(remote_hosts, nidb_node) remote_hosts = [start_node.tap.ip] result = measure.send(nidb, command, remote_hosts) processed = [] for line in result: processed.append([anm['ipv4'].node(n) for n in line]) #TODO: move this into verify module verification_results = verify.igp_routes(anm, processed) processed_with_results = [] for line in processed: prefix = str(line[-1].subnet) try: result = verification_results[prefix] except KeyError: result = False # couldn't find prefix processed_with_results.append({ 'path': line, 'verified': result, }) autonetkit.update_http(anm, nidb) ank_messaging.highlight([], [], processed_with_results)
def collect_sh_ip_route(anm, nidb, start_node = None): if not start_node: start_node = random.choice([n for n in nidb.nodes("is_router")]) #TODO: move this to another module, eg measure import autonetkit.measure as measure import autonetkit.verify as verify import autonetkit command = 'vtysh -c "show ip route"' #TODO: make auto take tap ip if netkit platform node #TODO: auto make put into list if isinstance(remote_hosts, nidb_node) remote_hosts = [start_node.tap.ip] result = measure.send(nidb, command, remote_hosts) processed = [] for line in result: processed.append([anm['ipv4'].node(n) for n in line]) #TODO: move this into verify module verification_results = verify.igp_routes(anm, processed) processed_with_results = [] for line in processed: prefix = str(line[-1].subnet) try: result = verification_results[prefix] except KeyError: result = False # couldn't find prefix processed_with_results.append({ 'path': line, 'verified': result, }) autonetkit.update_http(anm, nidb) ank_messaging.highlight([], [], processed_with_results)
def apply_difference(nidb_a, nidb_diff, emulation_server): #TODO: batch node updates try: modified_nodes = nidb_diff['nodes']['m'] except KeyError: print "no nodes modified" modified_nodes = {} ank_messaging.highlight(modified_nodes, [], []) for node_id, node_data in modified_nodes.items(): nidb_node = nidb_a.node(node_id) #print nidb_node.tap.ip if "_interfaces" in node_data: for interface_id, interface_data in node_data['_interfaces'].items(): interface = nidb_node.interface(interface_id) if 'ospf_cost' in interface_data: cost_1 = interface_data['ospf_cost'][1] cost_2 = interface_data['ospf_cost'][2] #print "%s (%s) ospf_cost %s -> %s" % (interface.id, interface.description, #cost_1, cost_2) # TODO: use a template for this (even inline would be an improvement) #TODO: can vtysh take \n delimeted data? change_command = "\n".join([ "conf t", "interface %s" % interface.id, "ip ospf cost %s " % cost_2]) # run as vtysh -c from linux shell rather than vtysh shell change_command = 'vtysh -c "%s"' % change_command host = nidb_node.tap.ip commands = [{"host": host, "username": "******", "password": "******","connector": "netkit", "command": change_command, "vtysh": False}] for response in get_results(emulation_server, commands): print response["result"] # and now view changed result collect_command = "show ip ospf interface %s" % interface.id commands = [{"host": host, "username": "******", "password": "******","connector": "netkit", "command": collect_command, "vtysh": True}] for response in get_results(emulation_server, commands): print response["result"] log.info("Differences applied")
def apply_difference(nidb_a, nidb_diff): #TODO: batch node updates try: modified_nodes = nidb_diff['nodes']['m'] except KeyError: print "no nodes modified" modified_nodes = {} ank_messaging.highlight(modified_nodes, [], []) for node_id, node_data in modified_nodes.items(): nidb_node = nidb_a.node(node_id) #print nidb_node.tap.ip if "_interfaces" in node_data: for interface_id, interface_data in node_data['_interfaces'].items(): interface = nidb_node.interface(interface_id) if 'ospf_cost' in interface_data: cost_1 = interface_data['ospf_cost'][1] cost_2 = interface_data['ospf_cost'][2] #print "%s (%s) ospf_cost %s -> %s" % (interface.id, interface.description, #cost_1, cost_2) # TODO: use a template for this (even inline would be an improvement) #TODO: can vtysh take \n delimeted data? command = "\n".join([ "conf t", "interface %s" % interface.id, "ip ospf cost %s " % cost_2]) command = 'vtysh -c "%s"' % command remote_hosts = [nidb_node.tap.ip] measure.send(nidb_a, command, remote_hosts) command = "show ip ospf interface %s" % interface.id command = 'vtysh -c "%s"' % command measure.send(nidb_a, command, remote_hosts)
import glob import json import time import random import autonetkit.ank_messaging as ank_messaging import autonetkit.anm anm = autonetkit.anm.AbstractNetworkModel() anm.restore("measurement_sh_ip_route/anm_20130409_200932.json.gz") g_ipv4 = anm['ipv4'] g_ospf = anm['ospf'] while True: path = [] for x in range(3): path.append(random.sample(list(g_ospf.nodes()), 3)) print path #path = [n for n in path] processed = path nodes = [path[0][0]] print nodes, processed #processed = [] #processed = [[g_ipv4.node("8"), g_ipv4.node("4"), g_ipv4.node("1")]] autonetkit.update_http(anm) ank_messaging.highlight(nodes, [], processed) time.sleep(3)
command_list.append(cmd) #break for response in get_results(emulation_server, command_list): data = response["result"] print "response is", response if not response['success']: print "Skipping response" continue header, parsed_data = ank_process.process_textfsm(parse_template, data) print header, parsed_data extracted = ank_process.extract_route_from_parsed_routing_table(header, parsed_data) mapped = ank_process.reverse_map_routing(reverse_mappings, extracted) print "mapped", mapped for entry in mapped: print response["command"] original_command = json.loads(response["command"]) original_node = original_command["original_node"] protocol, cd, node = entry if node is None: #vis = [original_node, cd] # direct connection? continue else: vis = [original_node, node, cd] #TODO: netx compare print "vis", vis ank_messaging.highlight(path = vis ) import time time.sleep(5)
anm.restore("measurement_sh_ip_route/anm_20130409_200932.json.gz") g_ipv4 = anm['ipv4'] for json_file in sorted(glob.glob('measurement_sh_ip_route/*.json')): with open(json_file, "r") as fh: data = json.loads(fh.read()) #print data if not len(data): continue processed = [] for line in data: processed.append([g_ipv4.node(n) for n in line]) verification_results = autonetkit.verify.igp_routes(anm, processed) processed_with_results = [] for line in processed: prefix = str(line[-1].subnet) result = verification_results[prefix] processed_with_results.append({ 'path': line, 'verified': result, }) autonetkit.update_http(anm) ank_messaging.highlight(nodes, [], processed_with_results) raw_input("Press Enter to continue...")
graph_ospf = g_ospf._graph spf = nx.all_pairs_dijkstra_path(graph_ospf, weight="cost") print "SPF" print spf for node in nidb.routers(): host = node.tap.ip cmd = {"host": host, "username": "******", "password": "******","connector": "netkit", "original_node": str(node), "command": command} command_list.append(cmd) for response in get_results(emulation_server, command_list): data = response["result"] #print "response is", response header, parsed_data = ank_process.process_textfsm(parse_template, data) path = ank_process.extract_path_from_parsed_traceroute(header, parsed_data) #print "path", path mapped = ank_process.reverse_map_path(reverse_mappings, path) if len(mapped): print mapped original_command = json.loads(response["command"]) original_node = original_command["original_node"] mapped.insert(0, original_node) # prepend first hop destination = mapped[-1] #print "expected", original_node, "to", destination #print "spf is", spf[original_node][destination] #print "got", mapped ank_messaging.highlight(path = mapped )
import random import autonetkit.ank_messaging as ank_messaging import autonetkit.anm anm = autonetkit.anm.AbstractNetworkModel() anm.restore("measurement_sh_ip_route/anm_20130409_200932.json.gz") g_ipv4 = anm['ipv4'] g_ospf = anm['ospf'] while True: path = [] for x in range(3): path.append(random.sample(list(g_ospf.nodes()), 3)) print path #path = [n for n in path] processed = path nodes = [path[0][0]] print nodes, processed #processed = [] #processed = [[g_ipv4.node("8"), g_ipv4.node("4"), g_ipv4.node("1")]] autonetkit.update_http(anm) ank_messaging.highlight(nodes, [], processed) time.sleep(3)
for node in nidb.routers(): host = node.tap.ip cmd = { "host": host, "username": "******", "password": "******", "connector": "netkit", "original_node": str(node), "command": command } command_list.append(cmd) for response in get_results(emulation_server, command_list): data = response["result"] #print "response is", response header, parsed_data = ank_process.process_textfsm(parse_template, data) path = ank_process.extract_path_from_parsed_traceroute(header, parsed_data) #print "path", path mapped = ank_process.reverse_map_path(reverse_mappings, path) if len(mapped): print mapped original_command = json.loads(response["command"]) original_node = original_command["original_node"] mapped.insert(0, original_node) # prepend first hop destination = mapped[-1] #print "expected", original_node, "to", destination #print "spf is", spf[original_node][destination] #print "got", mapped ank_messaging.highlight(path=mapped)
anm = autonetkit.anm.AbstractNetworkModel() anm.restore("measurement_sh_ip_route/anm_20130409_200932.json.gz") g_ipv4 = anm['ipv4'] for json_file in sorted(glob.glob('measurement_sh_ip_route/*.json')): with open(json_file, "r") as fh: data = json.loads(fh.read()) #print data if not len(data): continue processed = [] for line in data: processed.append([g_ipv4.node(n) for n in line]) verification_results = autonetkit.verify.igp_routes(anm, processed) processed_with_results = [] for line in processed: prefix = str(line[-1].subnet) result = verification_results[prefix] processed_with_results.append({ 'path': line, 'verified': result, }) autonetkit.update_http(anm) ank_messaging.highlight(nodes, [], processed_with_results) raw_input("Press Enter to continue...")
def starting_host(protocol, index, data): import autonetkit.ank_messaging as msg log.info("Starting %s" % data.group(1)) msg.highlight([data.group(1)], [], [])