def node_sort(upper_limit, lower_limit, location_code, file_append): with open ('data/nodes-%s.txt' %file_append, 'r') as nodes_file: devices = nodes_file.read().splitlines() nodes_dict = layers.nodes_dict() layers_key = layers.layers_key() for i in range (0, len(devices)): device_type = devices[i][devices[i].find("-") + 1 : devices[i].find("-") + 3] node_location_code = devices[i][0:len(location_code)] if device_type in ['ma','lc','ic','sc']: device_type = 'cc' elif device_type in ['ra','ea','ha']: device_type = 'a1' elif device_type in ['la','da','sa','ia']: device_type = 'a2' elif device_type not in layers.layers_list(): print "Device Type '%s' not found" %device_type continue if (node_location_code != location_code) and (location_code != 'all'): continue elif (int(layers.layers_dict(device_type)) < int(layers.layers_dict(upper_limit))) or (int(layers.layers_dict(device_type)) > int(layers.layers_dict(lower_limit))): continue else: nodes_dict[device_type].append(devices[i]) for i in nodes_dict: for j in nodes_dict[i]: nodes_dict[i] = natsort.natsorted(nodes_dict[i]) return nodes_dict
def lower_limit(upper_lim): layers_list = layers.layers_list() print "\nEnter lower limit of device hierarchy. Valid inputs include:", ','.join(layers_list[int(layers.layers_dict(upper_lim)):len(layers_list)]) lower_limit = raw_input("Lower limit: ") if ((len(lower_limit) is 0) or (lower_limit not in layers_list) or (int(layers.layers_dict(lower_limit)) <= int(layers.layers_dict(upper_lim)))): while (len(lower_limit) is 0) or (lower_limit not in layers_list) or (int(layers.layers_dict(lower_limit)) <= int(layers.layers_dict(upper_lim))): lower_limit = raw_input("Enter a valid lower limit: ") return lower_limit
def multi_access(next_nodes, output_nodes, output_links): global count count+=1 processes = [] filtered_next_nodes = [] # print "\n\nNodes from multi_access:", nodes # print "\n\tnext_nodes from multi_access:", next_nodes, "(%d)" %count # Empty queue if not already empty while not output_nodes.empty(): output_nodes.get() for i in range (len(next_nodes)): node_device_type = next_nodes[i][next_nodes[i].find("-") + 1 : next_nodes[i].find("-") + 3] node_location_code = next_nodes[i][0:len(location_code)] if (node_location_code != location_code) and (location_code != 'all'): continue if layers.layers_dict(node_device_type) == None: print "Node type not found,", next_nodes[i] continue # print "\n\tnext_nodes from multi_access:", next_nodes[i] if (int(layers.layers_dict(node_device_type)) >= int(layers.layers_dict(upper_limit)) and int(layers.layers_dict(node_device_type)) <= int(layers.layers_dict(lower_limit))): filtered_next_nodes.append(next_nodes[i]) for i in range (len(filtered_next_nodes)): process = mp.Process(target=discover.get_ndp_output, args=(filtered_next_nodes[i],username,password,file_append,output_nodes,output_links)) process.daemon=True processes.append(process) all_processes.append(process) text_display = "\r" + str(len(nodes)) + " devices found" sys.stdout.write(text_display) sys.stdout.flush() i=0 for p in processes: # print filtered_next_nodes[i], p.name, "starting" p.start() i+=1 # get process results from the output queue results = [output_nodes.get() for p in processes] for j in range (0,len(results)): # print "\n\nResults:", j, results[j], len(results), len(results[j]) if len(results[j]) > 0: next_access(results[j],output_nodes,output_links) text_display = "\r" + str(len(nodes)) + " devices found" sys.stdout.write(text_display) sys.stdout.flush()
def multi_access(next_nodes, output_nodes, output_links): global count count += 1 processes = [] filtered_next_nodes = [] # print "\n\nNodes from multi_access:", nodes # print "\n\tnext_nodes from multi_access:", next_nodes, "(%d)" %count # Empty queue if not already empty while not output_nodes.empty(): output_nodes.get() for i in range(len(next_nodes)): node_device_type = next_nodes[i][next_nodes[i].find("-") + 1:next_nodes[i].find("-") + 3] node_location_code = next_nodes[i][0:len(location_code)] if (node_location_code != location_code) and (location_code != 'all'): continue if layers.layers_dict(node_device_type) == None: print "Node type not found,", next_nodes[i] continue # print "\n\tnext_nodes from multi_access:", next_nodes[i] if (int(layers.layers_dict(node_device_type)) >= int( layers.layers_dict(upper_limit)) and int(layers.layers_dict(node_device_type)) <= int( layers.layers_dict(lower_limit))): filtered_next_nodes.append(next_nodes[i]) for i in range(len(filtered_next_nodes)): process = mp.Process(target=discover.get_ndp_output, args=(filtered_next_nodes[i], username, password, file_append, output_nodes, output_links)) process.daemon = True processes.append(process) all_processes.append(process) text_display = "\r" + str(len(nodes)) + " devices found" sys.stdout.write(text_display) sys.stdout.flush() i = 0 for p in processes: # print filtered_next_nodes[i], p.name, "starting" p.start() i += 1 # get process results from the output queue results = [output_nodes.get() for p in processes] for j in range(0, len(results)): # print "\n\nResults:", j, results[j], len(results), len(results[j]) if len(results[j]) > 0: next_access(results[j], output_nodes, output_links) text_display = "\r" + str(len(nodes)) + " devices found" sys.stdout.write(text_display) sys.stdout.flush()
def plot(upper_limit, lower_limit, location_code, file_append, interface_labels, graph_type): with open('data/links_scoped-%s.txt' % file_append, 'r') as links_file: lines = links_file.readlines() g1 = pydot.Dot(graph_type='graph', splines=graph_type, ranksep="6.0", nodesep="0.3") for j in range(0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1:delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1:delimited[1].find("-") + 3] if (int(layers.layers_dict(device_type_1)) < int( layers.layers_dict(upper_limit))) or (int( layers.layers_dict(device_type_2)) < int( layers.layers_dict(upper_limit))): continue if (int(layers.layers_dict(device_type_1)) > int( layers.layers_dict(lower_limit))) or (int( layers.layers_dict(device_type_2)) > int( layers.layers_dict(lower_limit))): continue if interface_labels is 'y': t_label = str(delimited[2]) h_label = str(delimited[3]) else: t_label = ' ' h_label = ' ' edge = pydot.Edge(str(delimited[0]), str(delimited[1]), color="black", fontsize='10', minlen="2.5", penwidth="1.0", labeldistance="1.2", taillabel=t_label, headlabel=h_label) g1.add_edge(edge) nodes = node_sort.node_sort(upper_limit, lower_limit, location_code, file_append) layer = pydot.Subgraph(rank='same') h_node = hlink_detect.hlink_detect(upper_limit, lower_limit, location_code, file_append) for i in range(0, len(nodes)): layers_key = layers.layers_key()[i] list = nodes[str(layers_key)] if len(list) != 0: if (layers_key == 'ec'): v_shape = "box3d" v_width = "3.0" v_height = "2.0" v_penwidth = "1.1" v_style = "rounded, filled, bold" v_align = "center" v_fontsize = "20.0" v_fontname = "arial" elif (layers_key == 'ed'): v_shape = "box3d" v_width = "3.0" v_height = "2.0" v_penwidth = "1.1" v_style = "rounded, filled, bold" v_align = "center" v_fontsize = "20.0" v_fontname = "arial" elif (layers_key == 'a1') or (layers_key == 'a2'): v_shape = "box3d" v_width = "1.0" v_height = "0.5" v_penwidth = "0.8" v_style = "rounded, filled, bold" v_align = "center" v_fontsize = "12.0" v_fontname = "arial" else: v_shape = "box3d" v_width = "3.0" v_height = "2.0" v_penwidth = "1.1" v_style = "rounded, filled, bold" v_align = "center" v_fontsize = "20.0" v_fontname = "arial" for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape=v_shape, width=v_width, height=v_height, penwidth=v_penwidth, style=v_style, align=v_align, fontsize=v_fontsize, fontname=v_fontname) layer.add_node(node) g1.add_subgraph(layer) layer = pydot.Subgraph(rank='same') if len(list) != 0: for k in range(0, len(list) - 1): if list[k] not in h_node: edge = pydot.Edge(str(list[k]), str(list[k + 1]), minlen="1.0", style='invis') g1.add_edge(edge) g1.write_svg('img/topology-%s.svg' % file_append)
def link_scope(upper_limit, lower_limit, location_code, file_append): with open('data/links_sorted-%s.txt' % file_append, 'r') as links_file: lines = links_file.readlines() scoped_file = open('data/links_scoped-%s.txt' % file_append, 'a') scoped_file.seek(0) scoped_file.truncate() nodes = node_sort.node_sort(upper_limit, lower_limit, location_code, file_append) both_found = 0 k1 = 0 k2 = 0 for i in range(0, len(lines)): delimited = lines[i].split() device_type_1 = delimited[0][delimited[0].find("-") + 1:delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1:delimited[1].find("-") + 3] node_location_code_1 = delimited[0][0:len(location_code)] node_location_code_2 = delimited[1][0:len(location_code)] if location_code != 'all': if (node_location_code_1 != location_code) or (node_location_code_2 != location_code): continue if ((int(layers.layers_dict(device_type_1)) < int( layers.layers_dict(upper_limit))) or (int(layers.layers_dict(device_type_2)) < int( layers.layers_dict(upper_limit)))): continue if ((int(layers.layers_dict(device_type_1)) > int( layers.layers_dict(lower_limit))) or (int(layers.layers_dict(device_type_2)) > int( layers.layers_dict(lower_limit)))): continue if int(layers.layers_dict(device_type_1)) < int( layers.layers_dict(device_type_2)): new_delimited = delimited[0] + "\t" + delimited[ 1] + "\t" + delimited[2] + "\t" + delimited[3] + "\n" scoped_file.write(new_delimited) elif int(layers.layers_dict(device_type_1)) > int( layers.layers_dict(device_type_2)): new_delimited = delimited[1] + "\t" + delimited[ 0] + "\t" + delimited[3] + "\t" + delimited[2] + "\n" scoped_file.write(new_delimited) elif int(layers.layers_dict(device_type_1)) == int( layers.layers_dict(device_type_2)): for key in nodes.keys(): if delimited[0] in nodes[key]: k1 = nodes[key].index(delimited[0]) both_found += 1 if delimited[1] in nodes[key]: k2 = nodes[key].index(delimited[1]) both_found += 1 if both_found is 2: both_found = 0 break if k1 < k2: new_delimited = delimited[0] + "\t" + delimited[ 1] + "\t" + delimited[2] + "\t" + delimited[3] + "\n" scoped_file.write(new_delimited) elif k1 > k2: new_delimited = delimited[1] + "\t" + delimited[ 0] + "\t" + delimited[3] + "\t" + delimited[2] + "\n" scoped_file.write(new_delimited) scoped_file.close() for line in fileinput.FileInput('data/links_scoped-%s.txt' % file_append, inplace=1): line = line.replace(":", "-") print line,
graph_type = info.graph_type() link_sort.link_scope(upper_limit, lower_limit, location_code, file_append) with open ('data/links_scoped-%s.txt' %file_append, 'r') as links_file: lines = links_file.readlines() g1 = pydot.Dot (graph_type = 'graph', splines=graph_type, ranksep="6.0", nodesep="0.3") for j in range (0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1 : delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1 : delimited[1].find("-") + 3] if (int(layers.layers_dict(device_type_1)) < int(layers.layers_dict(upper_limit))) or (int(layers.layers_dict(device_type_2)) < int(layers.layers_dict(upper_limit))): continue if (int(layers.layers_dict(device_type_1)) > int(layers.layers_dict(lower_limit))) or (int(layers.layers_dict(device_type_2)) > int(layers.layers_dict(lower_limit))): continue if interface_labels is 'y': t_label=str(delimited[2]) h_label=str(delimited[3]) else: t_label=' ' h_label=' ' edge = pydot.Edge (str(delimited[0]), str(delimited[1]), color="black", fontsize='10',
location_code = raw_input("Enter site code: ") link_sort.link_sort(upper_limit, lower_limit, location_code, file_append) with open ('data/links_sorted-%s.txt' %file_append, 'r') as links_file: lines = links_file.readlines() g1 = pydot.Dot (graph_type = 'graph', splines=info.graph_type(), ranksep="8.5", nodesep="0.01") for j in range (0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1 : delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1 : delimited[1].find("-") + 3] if (layers.layers_dict(device_type_1) < layers.layers_dict(upper_limit)) or (layers.layers_dict(device_type_2) < layers.layers_dict(upper_limit)): continue if (layers.layers_dict(device_type_1) > layers.layers_dict(lower_limit)) or (layers.layers_dict(device_type_2) > layers.layers_dict(lower_limit)): continue edge = pydot.Edge (str(delimited[0]), str(delimited[1]), color="black", fontsize='10', minlen="3.0", penwidth="0.4", labeldistance="1.2") g1.add_edge (edge) nodes = node_sort.node_sort(upper_limit, lower_limit, location_code, file_append)
def plot (upper_limit,lower_limit, location_code, file_append, interface_labels, graph_type): with open ('data/links_scoped-%s.txt' %file_append, 'r') as links_file: lines = links_file.readlines() g1 = pydot.Dot (graph_type = 'graph', splines=graph_type, ranksep="6.0", nodesep="0.3") for j in range (0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1 : delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1 : delimited[1].find("-") + 3] if (int(layers.layers_dict(device_type_1)) < int(layers.layers_dict(upper_limit))) or (int(layers.layers_dict(device_type_2)) < int(layers.layers_dict(upper_limit))): continue if (int(layers.layers_dict(device_type_1)) > int(layers.layers_dict(lower_limit))) or (int(layers.layers_dict(device_type_2)) > int(layers.layers_dict(lower_limit))): continue if interface_labels is 'y': t_label=str(delimited[2]) h_label=str(delimited[3]) else: t_label=' ' h_label=' ' edge = pydot.Edge (str(delimited[0]), str(delimited[1]), color="black", fontsize='10', minlen="2.5", penwidth="1.0", labeldistance="1.2", taillabel=t_label, headlabel=h_label) g1.add_edge (edge) nodes = node_sort.node_sort(upper_limit,lower_limit, location_code, file_append) layer = pydot.Subgraph(rank='same') h_node = hlink_detect.hlink_detect(upper_limit,lower_limit, location_code, file_append) for i in range (0, len(nodes)): layers_key = layers.layers_key()[i] list = nodes[str(layers_key)] if len(list) != 0: if (layers_key == 'ec'): v_shape="box3d" v_width="3.0" v_height="2.0" v_penwidth="1.1" v_style="rounded, filled, bold" v_align="center" v_fontsize="20.0" v_fontname="arial" elif (layers_key == 'ed'): v_shape="box3d" v_width="3.0" v_height="2.0" v_penwidth="1.1" v_style="rounded, filled, bold" v_align="center" v_fontsize="20.0" v_fontname="arial" elif (layers_key == 'a1') or (layers_key == 'a2'): v_shape="box3d" v_width="1.0" v_height="0.5" v_penwidth="0.8" v_style="rounded, filled, bold" v_align="center" v_fontsize="12.0" v_fontname="arial" else: v_shape="box3d" v_width="3.0" v_height="2.0" v_penwidth="1.1" v_style="rounded, filled, bold" v_align="center" v_fontsize="20.0" v_fontname="arial" for k in range (0, len(list)): node = pydot.Node(str(list[k]), shape=v_shape, width=v_width, height=v_height, penwidth=v_penwidth, style=v_style, align=v_align, fontsize=v_fontsize, fontname=v_fontname) layer.add_node(node) g1.add_subgraph(layer) layer = pydot.Subgraph(rank='same') if len(list) != 0: for k in range (0, len(list)-1): if list[k] not in h_node: edge = pydot.Edge (str(list[k]), str(list[k+1]), minlen="1.0", style='invis') g1.add_edge (edge) g1.write_svg('img/topology-%s.svg' %file_append)
def link_scope(upper_limit, lower_limit, location_code, file_append): with open ('data/links_sorted-%s.txt' %file_append, 'r') as links_file: lines = links_file.readlines() scoped_file = open ('data/links_scoped-%s.txt' %file_append, 'a') scoped_file.seek(0) scoped_file.truncate() nodes = node_sort.node_sort(upper_limit, lower_limit, location_code, file_append) both_found = 0 k1 = 0 k2 = 0 for i in range (0, len(lines)): delimited = lines[i].split() device_type_1 = delimited[0][delimited[0].find("-") + 1 : delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1 : delimited[1].find("-") + 3] node_location_code_1 = delimited[0][0:len(location_code)] node_location_code_2 = delimited[1][0:len(location_code)] if location_code != 'all': if (node_location_code_1 != location_code) or (node_location_code_2 != location_code): continue if ((int(layers.layers_dict(device_type_1)) < int(layers.layers_dict(upper_limit))) or (int(layers.layers_dict(device_type_2)) < int(layers.layers_dict(upper_limit)))): continue if ((int(layers.layers_dict(device_type_1)) > int(layers.layers_dict(lower_limit))) or (int(layers.layers_dict(device_type_2)) > int(layers.layers_dict(lower_limit)))): continue if int(layers.layers_dict(device_type_1)) < int(layers.layers_dict(device_type_2)): new_delimited = delimited[0] + "\t" + delimited[1] + "\t" + delimited[2] + "\t" + delimited[3] + "\n" scoped_file.write(new_delimited) elif int(layers.layers_dict(device_type_1)) > int(layers.layers_dict(device_type_2)): new_delimited = delimited[1] + "\t" + delimited[0] + "\t" + delimited[3] + "\t" + delimited[2] + "\n" scoped_file.write(new_delimited) elif int(layers.layers_dict(device_type_1)) == int(layers.layers_dict(device_type_2)): for key in nodes.keys(): if delimited[0] in nodes[key]: k1 = nodes[key].index(delimited[0]) both_found+=1 if delimited[1] in nodes[key]: k2 = nodes[key].index(delimited[1]) both_found+=1 if both_found is 2: both_found = 0 break if k1 < k2: new_delimited = delimited[0] + "\t" + delimited[1] + "\t" + delimited[2] + "\t" + delimited[3] + "\n" scoped_file.write(new_delimited) elif k1 > k2: new_delimited = delimited[1] + "\t" + delimited[0] + "\t" + delimited[3] + "\t" + delimited[2] + "\n" scoped_file.write(new_delimited) scoped_file.close() for line in fileinput.FileInput('data/links_scoped-%s.txt' %file_append, inplace=1): line = line.replace(":","-") print line,
lines = links_file.readlines() g1 = pydot.Dot(graph_type='graph', splines=info.graph_type(), ranksep="8.5", nodesep="0.01") for j in range(0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1:delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1:delimited[1].find("-") + 3] if (layers.layers_dict(device_type_1) < layers.layers_dict(upper_limit) ) or (layers.layers_dict(device_type_2) < layers.layers_dict(upper_limit)): continue if (layers.layers_dict(device_type_1) > layers.layers_dict(lower_limit) ) or (layers.layers_dict(device_type_2) > layers.layers_dict(lower_limit)): continue edge = pydot.Edge(str(delimited[0]), str(delimited[1]), color="black", fontsize='10', minlen="3.0", penwidth="0.4",
def plot(upper_limit, lower_limit, location_code, file_append): with open('data/links_sorted-%s.txt' % file_append, 'r') as links_file: lines = links_file.readlines() g1 = pydot.Dot(graph_type='graph', splines='spline', ranksep="8.5", nodesep="0.01") for j in range(0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1:delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1:delimited[1].find("-") + 3] if (int(layers.layers_dict(device_type_1)) < int( layers.layers_dict(upper_limit))) or (int( layers.layers_dict(device_type_2)) < int( layers.layers_dict(upper_limit))): continue if (int(layers.layers_dict(device_type_1)) > int( layers.layers_dict(lower_limit))) or (int( layers.layers_dict(device_type_2)) > int( layers.layers_dict(lower_limit))): continue edge = pydot.Edge(str(delimited[0]), str(delimited[1]), color="black", fontsize='10', minlen="3.0", penwidth="0.4", labeldistance="1.2") g1.add_edge(edge) nodes = node_sort.node_sort(upper_limit, lower_limit, location_code) layer = pydot.Subgraph(rank='same') h_node = hlink_detect.hlink_detect(upper_limit, lower_limit, location_code, file_append) for i in range(0, len(nodes)): layers_key = layers.layers_key()[i] list = nodes[str(layers_key)] if len(list) != 0: if (layers_key == 'br') or (layers_key == 'xr'): for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="record", width="15.0", height="4.0", penwidth="10.0", style="rounded, filled, bold", align="center", fontsize="120.0", fontname="arial") layer.add_node(node) elif (layers_key == 'ar'): for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="oval", width="10.0", height="2.5", penwidth="10.0", style="rounded, filled, bold", align="center", fontsize="100.0", fontname="arial") layer.add_node(node) elif (layers_key == 'ec'): for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="record", width="10.0", height="2.5", penwidth="10.0", style="rounded, filled, bold", align="center", fontsize="100.0", fontname="arial") layer.add_node(node) elif (layers_key == 'ed'): for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="record", width="4.5", height="2.0", penwidth="2.0", style="rounded, filled, bold", align="center", fontsize="40.0", fontname="arial") layer.add_node(node) elif (layers_key == 'a1') or (layers_key == 'a2'): for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="record", width="0.1", height="0.1", penwidth="3.0", color="white", style="rounded, filled, bold", align="center", fontsize="5.0", fontname="arial") layer.add_node(node) else: for k in range(0, len(list)): node = pydot.Node(str(list[k]), shape="record", width="10.0", height="2.5", penwidth="10.0", style="rounded, filled, bold", align="center", fontsize="100.0", fontname="arial") layer.add_node(node) g1.add_subgraph(layer) layer = pydot.Subgraph(rank='same') if len(list) != 0: for k in range(0, len(list) - 1): if list[k] not in h_node: edge = pydot.Edge(str(list[k]), str(list[k + 1]), minlen="2.0", style='invis') g1.add_edge(edge) g1.write_svg('img/topology-%s.svg' % file_append)
lines = links_file.readlines() g1 = pydot.Dot(graph_type='graph', splines=graph_type, ranksep="6.0", nodesep="0.3") for j in range(0, len(lines)): delimited = lines[j].split() device_type_1 = delimited[0][delimited[0].find("-") + 1:delimited[0].find("-") + 3] device_type_2 = delimited[1][delimited[1].find("-") + 1:delimited[1].find("-") + 3] if (int(layers.layers_dict(device_type_1)) < int( layers.layers_dict(upper_limit))) or (int( layers.layers_dict(device_type_2)) < int( layers.layers_dict(upper_limit))): continue if (int(layers.layers_dict(device_type_1)) > int( layers.layers_dict(lower_limit))) or (int( layers.layers_dict(device_type_2)) > int( layers.layers_dict(lower_limit))): continue if interface_labels is 'y': t_label = str(delimited[2]) h_label = str(delimited[3]) else: