如何在同一代码中使用NAPALM中的get_interfaces_ip()和get_conf()功能



我在这段代码中的目的是检测";"虚拟以太网";NE40华为设备中的接口。我能够完成这一步。正如我在下面提到的,我的问题是,如何使用get_device((或其他方法查看这些接口的配置?还是我应该使用Napalm的不同库?

代码的第一部分:

from napalm import get_network_driver
driver = get_network_driver('huawei_vrp')
device = driver(hostname='X.X.X.X', username='admin', password='admin')
device.open()
get_interfaces_ip = device.get_interfaces_ip()
get_config_device = device.get_config()
for i in get_interfaces_ip:
if "Virtual-Ether" in i:
print(i)

打印(i(输出:

Virtual-Ethernet3/0/1.3083
Virtual-Ethernet3/0/1.3086
Virtual-Ethernet3/0/1.3087
Virtual-Ethernet3/0/1.3090
Virtual-Ethernet3/0/1.3091

完整代码:(在代码中添加了get_config((功能(

from napalm import get_network_driver
driver = get_network_driver('huawei_vrp')
device = driver(hostname='X.X.X.X', username='admin', password='admin')
device.open()
get_interfaces_ip = device.get_interfaces_ip()
get_config_device = device.get_config()
for i in get_interfaces_ip:
if "Virtual-Ether" in i:
#print(i)
interface_config = get_config_device[i]
print(interface_config)

print(interface_config(收到错误:

[appadmin@ryugbz01 Huawei]$ python3 napalm_vrp_huawei_conf.py
Virtual-Ethernet3/0/1.24
Traceback (most recent call last):
File "napalm_vrp_huawei_conf.py", line 19, in <module>
interface_config = get_config_device[i]
KeyError: 'Virtual-Ethernet3/0/1.24'

在最后一部分中,使用get_config((功能,我看不到Virtual-Ethernet3/0/1.24接口下的配置。我应该采用什么方法?

也许下面的脚本可以给出一个想法。

#To install napalm-ce you just have to run the following command: 'pip install napalm-ce'
import napalm
from tabulate import tabulate

def main():

driver_vrp = napalm.get_network_driver("ce")
device_list = [["vrp-sw1","vrp", "switch"],["vrp-r1", "vrp", "router"]]

network_devices = []
for device in device_list:
network_devices.append(
driver_vrp(
hostname = device[0],
username = "admin",
password = "admin"
)
)
devices_table = [["hostname", "vendor", "model", "uptime", "serial_number"]]
devices_int_ip_table = [["hostname","interface","is_up","ipv4/mask"]]

for device in network_devices:
print("Connecting to {} ...".format(device.hostname))
device.open()
print("Getting device facts")
device_facts = device.get_facts()

devices_table.append([device_facts["hostname"]
])
print("Getting physical interfaces ip")
device_interfaces_ip = device.get_interfaces_ip()
for interface in device_interfaces_ip:
if device_interfaces[interface]['is_up']:
ip_address = list(device_interfaces_ip[interface]['ipv4'].keys())[0]
devices_int_ip_table.append([device_facts["hostname"],
interface,
device_interfaces[interface]['is_up'],
"{}/{}".format(ip_address,
device_interfaces_ip[interface]['ipv4'][ip_address]['prefix_length']),
])

print("Getting device config")
device_config = device.get_config()
device.close()
print("Done.")
print(tabulate(devices_int_ip_table, headers="firstrow"))
print()
print(device_config)

if __name__ == '__main__':
main()

相关内容

  • 没有找到相关文章

最新更新