def check_ospf_full_adjacencies(dev, neighbor_count): # (4) ospf_table = OspfNeighborTable(dev) # Create an instance of the Table ospf_table.get() # Populate the Table if len(ospf_table) != neighbor_count: return False for neighbor in ospf_table: if neighbor["ospf_neighbor_state"] != "Full": return False return True
class Neighbors(Util): """Junos Neighbor Class :param dev: connected device :type dev: jnpr.junos.Device :reises: jnpr.junos.exception """ @property def lldp(self): if not hasattr(self, '_lldp'): self._lldp = LLDPNeighborTable(self.dev) return self._lldp.get() @property def isis(self): if not hasattr(self, '_isis'): self._isis = IsisAdjacencyTable(self.dev) return self._isis.get() @property def ospf(self): if not hasattr(self, '_ospf'): self._ospf = OspfNeighborTable(self.dev) return self._ospf.get() def all(self): """Return ALL Neighbors with protocols :return: dict of neighbors with IFD as key :rtype: dict """ neighbors = defaultdict(lambda: {'protocols': set()}) for lldp in self.lldp: neighbors[lldp.local_int]['hostname'] = lldp.remote_sysname neighbors[lldp.local_int]['protocols'].add('lldp') for isis in self.isis: ifd, unit = isis.interface_name.split('.') if 'hostname' not in neighbors[ifd]: neighbors[ifd]['hostname'] = isis.system_name neighbors[ifd]['protocols'].add('isis') for ospf in self.ospf: ifd, unit = ospf.interface_name.split('.') if 'hostname' not in neighbors[ifd]: try: dns_name = socket.gethostbyaddr(ospf.neighbor_id)[0] neighbors[ifd]['hostname'] = dns_name except socket.herror: neighbors[ifd]['hostname'] = ospf.neighbor_id neighbors[ifd]['protocols'].add('ospf') return neighbors def display(self): """Display ALL Neighbors""" print("Neighbors:") print("%-16s %-16s %s" % ('Interface', 'Hostname', 'Protocols')) for ifd, attributes in self.all().iteritems(): print("%-16s %-16s %s" % (ifd, attributes['hostname'], ", ".join(attributes['protocols'])))
def get_full_neighbor_count(host): count = 0 with Device(host=host, user=junos_username, passwd=junos_password, port=22) as dev: ospf_table = OspfNeighborTable(dev).get() for neighbor in ospf_table: if neighbor.ospf_neighbor_state == "Full": count += 1 return count
def get_full_neighbor_count(host): count = 0 with Device(host=host, user=USER, passwd=PASSWD) as dev: ospf_table = OspfNeighborTable(dev).get() for neighbor in ospf_table: if neighbor.ospf_neighbor_state == "Full": count += 1 return count
def ospf(dev, instance=None): print( f"{Fore.YELLOW}{_create_header('begin troubleshoot ospf')}{Style.RESET_ALL}\n" ) if instance: neighbors = OspfNeighborTable(dev).get(instance=instance) interfaces = OspfInterfaceTable(dev).get(instance=instance) else: neighbors = OspfNeighborTable(dev).get() interfaces = OspfInterfaceTable(dev).get() for interface in interfaces: if interface.passive: passive = 'yes' else: passive = 'no' print( f"Interface: {interface.interface_name:21} Neighbor Count: {interface.neighbor_count}\n" f" Passive: {passive}") print(" Neighbors:") for neighbor in neighbors: if interface.interface_name == neighbor.interface_name: if neighbor.ospf_neighbor_state != "Full": print( f" {Fore.RED}{neighbor.neighbor_address:15} Uptime: {str(neighbor.neighbor_up_time):15}" f"Neighbor state: {neighbor.ospf_neighbor_state}{Style.RESET_ALL}" ) else: print( f" {neighbor.neighbor_address:15} Uptime: {neighbor.neighbor_up_time}" ) routes = RouteSummaryTable(dev).get() total_routes = 0 for route in routes: if route.proto['OSPF']: print( f"Table: {route.name} routes:{route.proto['OSPF'].count} active:{route.proto['OSPF'].active}" ) total_routes = total_routes + route.proto['OSPF'].count print(f"Total OSPF Routes: {total_routes}") print( f"{Fore.YELLOW}{_create_header('end of troubleshoot ospf')}{Style.RESET_ALL}\n" )
def ospf(self): if not hasattr(self, '_ospf'): self._ospf = OspfNeighborTable(self.dev) return self._ospf.get()