napalm 简单介绍

napalm (Network Automation and Programmability Abstraction Layer with Multivendor support)顾名思义,是一个给多个厂家设备都抽象了相同名字方法的 网络自动化 python库. 官方参考文档:https://napalm.readthedocs.io/en/latest/   一、导入napalm ```python from napalm import * ``` 表示导入naplam这个包,只会导入2个东西(python中__all__决定的): ```python ##关键源码 napalm/__init__.py __all__ = ("get_network_driver", "SUPPORTED_DRIVERS") ``` get_network_driver 表示获取具体厂商驱动的 方法 SUPPORTED_DRIVERS 表示驱动已经支持哪些厂家,是一个列表, 后续自定义了驱动 把名字加上即可   二、获取厂家(设备型号)驱动 获取具体厂商的驱动(以思科的ios为例): ```python from napalm import * ios_driver = get_network_driver(‘ios‘) ``` get_network_driver 根据输入的驱动名字来导入对应的包(napalm实际上是一个包含多个包的包) ```python ## 关键代码 napalm/base/__init__.py def get_network_driver(name, prepend=True): if name == "mock": return MockDriver # 这里主要是对输入内容合法性进行过滤 # if not (isinstance(name, str) and len(name) > 0): raise ModuleImportError("Please provide a valid driver name.") # 根据输入对驱动名进行导包 importlib # # Only lowercase allowed name = name.lower() # Try to not raise error when users requests IOS-XR for e.g. module_install_name = name.replace("-", "") community_install_name = "napalm_{name}".format(name=module_install_name) custom_install_name = "custom_napalm.{name}".format(name=module_install_name) # Can also request using napalm_[SOMETHING] if "napalm" not in module_install_name and prepend is True: module_install_name = "napalm.{name}".format(name=module_install_name) # Order is custom_napalm_os (local only) -> napalm.os (core) -> napalm_os (community) for module_name in [ custom_install_name, module_install_name, community_install_name, ]: try: module = importlib.import_module(module_name) break except ImportError as e: message = str(e) if "No module named" in message: # py2 doesn‘t have ModuleNotFoundError exception failed_module = message.split()[-1] if failed_module.replace("‘", "") in module_name: continue raise e else: raise ModuleImportError( ‘Cannot import "{install_name}". Is the library installed?‘.format( install_name=name ) ) # 导入模块的对象中必须包含NetworkDriver子类(后续自定义驱动的话,也要继承NetworkDriver), 然后返回对应的 驱动class # for name, obj in inspect.getmembers(module): if inspect.isclass(obj) and issubclass(obj, NetworkDriver): return obj # 如果输入的驱动名字 没有匹配上,那么会抛出异常 # # looks like you don‘t have any Driver class in your module... raise ModuleImportError( ‘No class inheriting "napalm.base.base.NetworkDriver" found in "{install_name}".‘.format( install_name=module_install_name ) )``` ```   四、查看ios驱动实现了哪些方法 ```python from napalm import * ios_driver = get_network_driver(‘ios‘) print(list(i for i in dir(ios_driver) if not i.startswith(‘_‘))) # 查看ios_driver实现的方法 ``` 输出: ```python [‘bgp_time_conversion‘, ‘cli‘, ‘close‘, ‘commit_config‘, ‘compare_config‘, ‘compliance_report‘, ‘confirm_commit‘, ‘connection_tests‘, ‘dest_file_system‘, ‘discard_config‘, ‘get_arp_table‘, ‘get_bgp_config‘, ‘get_bgp_neighbors‘, ‘get_bgp_neighbors_detail‘, ‘get_config‘, ‘get_environment‘, ‘get_facts‘, ‘get_firewall_policies‘, ‘get_interfaces‘, ‘get_interfaces_counters‘, ‘get_interfaces_ip‘, ‘get_ipv6_neighbors_table‘, ‘get_lldp_neighbors‘, ‘get_lldp_neighbors_detail‘, ‘get_mac_address_table‘, ‘get_network_instances‘, ‘get_ntp_peers‘, ‘get_ntp_servers‘, ‘get_ntp_stats‘, ‘get_optics‘, ‘get_probes_config‘, ‘get_probes_results‘, ‘get_route_to‘, ‘get_snmp_information‘, ‘get_users‘, ‘get_vlans‘, ‘has_pending_commit‘, ‘is_alive‘, ‘load_merge_candidate‘, ‘load_replace_candidate‘, ‘load_template‘, ‘open‘, ‘parse_uptime‘, ‘ping‘, ‘post_connection_tests‘, ‘pre_connection_tests‘, ‘rollback‘, ‘traceroute‘] ```   五、实际上napalm在base包中,也就是父类NetworkDriver中定义了很多方法, 这些方法实际上是每个厂家驱动都要实现的,如果没有实现 调用时会抛出"为定义的错误"。 同时,NetworkDriver中定义的方法,也是我们后续自定义其他厂家驱动时 需要去覆盖实现的具体方法。 ```python import napalm list1 = list(i for i in dir(napalm.base.NetworkDriver) if not i.startswith(‘_‘)) print(list1) # output # [‘cli‘, ‘close‘, ‘commit_config‘, ‘compare_config‘, ‘compliance_report‘, ‘confirm_commit‘, ‘connection_tests‘, ‘discard_config‘, ‘get_arp_table‘, ‘get_bgp_config‘, ‘get_bgp_neighbors‘, ‘get_bgp_neighbors_detail‘, ‘get_config‘, ‘get_environment‘, ‘get_facts‘, ‘get_firewall_policies‘, ‘get_interfaces‘, ‘get_interfaces_counters‘, ‘get_interfaces_ip‘, ‘get_ipv6_neighbors_table‘, ‘get_lldp_neighbors‘, ‘get_lldp_neighbors_detail‘, ‘get_mac_address_table‘, ‘get_network_instances‘, ‘get_ntp_peers‘, ‘get_ntp_servers‘, ‘get_ntp_stats‘, ‘get_optics‘, ‘get_probes_config‘, ‘get_probes_results‘, ‘get_route_to‘, ‘get_snmp_information‘, ‘get_users‘, ‘get_vlans‘, ‘has_pending_commit‘, ‘is_alive‘, ‘load_merge_candidate‘, ‘load_replace_candidate‘, ‘load_template‘, ‘open‘, ‘ping‘, ‘post_connection_tests‘, ‘pre_connection_tests‘, ‘rollback‘, ‘traceroute‘]```

napalm 简单介绍

上一篇:0.618法 && 牛顿法


下一篇:vue下不同组件在其他元素div为绝对postion情况下进行侧边栏进行collapse变动情况处理