def gen_physical_ports(port_list): """ Find all enabled physical interfaces of this :param port_list: The list of all physical ports that should be analyzed :return: A list of Tuple (interface name, ip address) for each active physical interface """ ports = [] for port_name in port_list: try: out = subprocess.check_output(['ip', 'a', 'show', port_name]) for line in out.splitlines(): if 'inet ' in line: line = line.strip(' \t\n') # inet 130.104.228.87/25 brd 130.104.228.127 \ # scope global dynamic eno1 port_addr = ip_interface(line.split(' ')[1]) log.debug('Added physical port %s@%s', port_name, port_addr) ports.append((port_name, port_addr)) break # TODO support multiple IP/interface? except subprocess.CalledProcessError as e: log.exception(e) return ports
def gen_physical_ports(port_list): """ Find all enabled physical interfaces of this :param port_list: The list of all physical ports that should be analyzed :return: A list of Tuple (interface name, ip address) for each active physical interface """ ports = [] for port_name in port_list: try: ip = ip_interface(CFG.get(port_name, 'ip')) ports.append((port_name, ip)) except ConfigError: try: out = subprocess.check_output(['ip', 'a', 'show', port_name]) for line in out.splitlines(): if 'inet ' in line: line = line.strip(' \t\n') port_addr = ip_interface(line.split(' ')[1]) log.debug('Added physical port %s@%s', port_name, port_addr) ports.append((port_name, port_addr)) break # TODO support multiple IP/interface? except subprocess.CalledProcessError as e: log.exception(e) return ports
def refresh_augmented_topo(self): log.info('Solving topologies') try: self.optimizer.solve(self.igp_graph, self.fwd_dags) except Exception as e: log.exception(e) return self.advertized_lsa else: return set(self.optimizer.get_fake_lsas())
def refresh_augmented_topo(self): log.info('Solving topologies') if not self.json_proxy.alive() or not self.has_initial_topo: log.debug('Skipping as we do not yet have a topology') return self.advertized_lsa try: self.optimizer.solve(self.igp_graph, self.fwd_dags) except Exception as e: log.exception(e) return self.advertized_lsa else: return set(self.optimizer.get_fake_lsas())
def force(f, *args, **kwargs): """ Force the execution of function and log any exception :param f: the function to execute :param args: its arguments :param kwargs: its kw-arguments :return: the return value of f is any, or None """ try: return f(*args, **kwargs) except Exception as e: log.exception(e) return None
def remove_route(self, network): """ Remove a route :param network: The prefix to remove """ net = ip_network(network) try: route = self.routes[net] self.remove_route_part(net, *[p for p in route]) except KeyError: log.debug('No route for network %s', net) except Exception as other_exception: log.exception(other_exception)
if os.path.exists(output): os.unlink(output) plt.savefig(output) plt.close() log.debug('Graph of %d nodes saved in %s', len(graph), output) except: pass # The draw_graph call will be remapped to 'nothing' if matplotlib (aka extra # packages) is not available try: from networkx import spring_layout, draw_networkx_edge_labels, draw import matplotlib.pyplot as plt except ImportError as e: log.error('Missing packages to draw the network') log.exception(e) draw_graph = lambda x: True def contract_graph(graph, nodes, into): """ Contract the graph :param graph: The graph to contract :param nodes: The set of nodes to contract into one :param into: The (new) node that should be the contraction """ edges = graph.edges(nodes, data=True) graph.add_edges_from(map(lambda x: (into, x[1], x[2]), edges)) graph.remove_nodes_from(nodes)