pythonnetwork-programmingdevicehuawei-developersnapalm

How to use get_interfaces_ip() and get_conf() features in NAPALM in the same code


My purpose in this code is to detect interfaces with L3 IP under the "Virtual-ethernet" interfaces in a NE40 Huawei device. I was able to do this step. As I mentioned below, my question is, How can I see the configurations of these interfaces with get_device() or a different method? Or should I use a different library with Napalm?

First part of code :

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)

print(i) Output:

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

Full code: ( Added get_config() feature to code)

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) Error received :

[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'

In the last part, with the get_config() feature, I cannot see the configurations under Virtual-Ethernet3/0/1.24 interface, for example. What method should I apply?


Solution

  • Maybe the following script can give an idea.

    #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()