Table of Contents
NSO python examples are the topic of this section.
In the previous section, we started learning the python maagic library to navigate the configuration of network devices through cisco NSO.
In this section we will see some other practical python maagic code examples to show, change and delete configuration through Cisco NSO.
Cisco NSO python maagic library code examples
In continue, we will see some python code examples using maagic library to show or change the configurtaion of network devices,
iterate devices configured in cisco nso
In the first example discussed in the previous section, we navigated through the configuration of a specific device, but in the first example in this section we want to iterate through all the devices added in Cisco NSO dynamically and via Python code.
This is the code to iterate through the devices configured in Cisco NSO.
import ncs with ncs.maapi.single_read_trans("admin", "python") as t: root = ncs.maagic.get_root(t) for box in root.devices.device: print("devicename:", box.name) print("device model:", box.platform.model) print("device address:", root.devices.device[box.name].address) print("device authentication group:", root.devices.device[box.name].authgroup) print("!!!!!!!!!!!!!!!!!!!!!!!!!")
First, we create a read transaction to the root of Cisco NSO as we discussed in the previous section.
Then we use “root.devices.device” to iterate through the devices and for each device, we print out the device name, device model, device address, and device authentication group.
Let’s run the script and see the list of devices.
majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 2.device_iteration.py devicename: R1 device model: CSR1000V device address: 192.168.2.91 device authentication group: RAYKA !!!!!!!!!!!!!!!!!!!!!!!!! devicename: asa0 device model: NETSIM device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: ios0 device model: NETSIM device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: ios1 device model: NETSIM device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: ios2 device model: NETSIM device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: iosxr0 device model: NETSIM device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: junos0 device model: None device address: 127.0.0.1 device authentication group: default !!!!!!!!!!!!!!!!!!!!!!!!! devicename: nxos0 device model: NETSIM device address: 127.0.0.1 device authentication group: default
sync devices with "sync-from()" method
When you configure network devices through Cisco NSO, it is important that the configuration of the network devices is already synchronized in Cisco NSO.
As you know, “sync-from” and “sync-to” are two methods in Cisco NSO that you can use to sync configuration from device to Cisco NSO or vice versa.
This also applies when you configure network devices via Python code.
As an example, let’s change some configurations directly in the network device to unsync the Cisco NSO with the network device.
Then we run a python code to change some configuration in network device.
As we expect, we receive the error „out of sync“.
majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 4.add_configu.py Traceback (most recent call last): File "/home/majid/devnet/cisco_nso/12.cisco_nso_python_api/4.add_configu.py", line 15, in <module> t.apply() File "/home/majid/nso-6.0/src/ncs/pyapi/ncs/maapi.py", line 1979, in apply self.maapi.apply_trans_flags(self.th, keep_open, flags) File "/home/majid/nso-6.0/src/ncs/pyapi/ncs/maapi.py", line 375, in proxy return real(self2.msock, *args, **kwargs) _ncs.error.Error: Unknown error (65): Network Element Driver: device R1: out of sync
Therefore, in this example we will see how the configuration is synchronized in the Python code.
import ncs with ncs.maapi.single_read_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) device_object = root.devices.device["R1"] #print(dir(device_object)) result = device_object.sync_from()
majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 3.sync_from_devices.py majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 4.add_configu.py majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$
After getting read transaction access to the root of Cisco NSO, we connect to the device “R1” in which we are going to sync the configuration.
We then use the “sync_from()” method to synchronize the configuration from the network device to the Cisco NSO.
In the previous section, we learned how to extract the list of methods using the “dir” command.
change the configuration of network devices
Now that the configuration is synced between NSO and network devices, we can change the configuration via Python code.
This is the code example to enable the CDP protocol globally and also change the IP address of an interface.
import ncs with ncs.maapi.single_write_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) device_object = root.devices.device["R1"].config # enable cdp device_object.ios__cdp.run = True # add loopback interface device_object.ios__interface["Loopback"].create("1337") device_object.ios__interface.Loopback["1337"].ip.address.primary.address = "192.168.1.1" device_object.ios__interface.Loopback["1337"].ip.address.primary.mask = "255.255.255.252" # commit the configuration t.apply()
The difference is that we need to open a write transaction to root of Cisco NSO.
Then we connect to the device whose configuration we are going to change.
The other difference is that we need to enter to the “config” section of the device.
Then the best way to find the path to any part of configuration is “xpath”, which we discussed in the previous section.
Once we have the path, we can easily change the value via Python code like we did in this code to enable CDP and change the IP address of a loopback interface.
Finally we have to commit the new configuration via „apply“ command.
Let’s run the Python code and then directly check the router interfaces to make sure the new loopback interface is configured.
R1#show ip int brief Interface IP-Address OK? Method Status Protocol GigabitEthernet1 192.168.2.91 YES NVRAM up up GigabitEthernet2 unassigned YES NVRAM administratively down down GigabitEthernet3 unassigned YES NVRAM administratively down down Loopback100 4.5.6.7 YES NVRAM up up Loopback123 12.13.14.16 YES NVRAM up up Loopback135 1.3.5.7 YES NVRAM up up Loopback150 1.1.1.1 YES NVRAM up up Loopback1337 192.168.1.1 YES manual up up R1#
delete the configuration of network devices
Another important requirement to be met via python code is deleting a configuration section.
import ncs with ncs.maapi.single_write_trans('admin', 'python') as t: root = ncs.maagic.get_root(t) device_object = root.devices.device["R1"].config del device_object.ios__interface.Loopback["1337"] t.apply()
It is obvious that we need a write transaction to the device to delete a configuration.
Getting access to the nso and network device is like all other code examples.
The key point is to use „del“ command and then the path to reach the interesting part of the configuration. As always, the path to the configuration is extracted using the nso xpath option.
Finally we commit the configuration using „apply“ command.
Let’s run the script and then make sure that the new loopback interface is deleted from network device.
majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 5.delete_config.py majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$
R1#show ip int brief Interface IP-Address OK? Method Status Protocol GigabitEthernet1 192.168.2.91 YES NVRAM up up GigabitEthernet2 unassigned YES NVRAM administratively down down GigabitEthernet3 unassigned YES NVRAM administratively down down Loopback100 4.5.6.7 YES NVRAM up up Loopback123 12.13.14.16 YES NVRAM up up Loopback135 1.3.5.7 YES NVRAM up up Loopback150 1.1.1.1 YES NVRAM up up
show command through "live-status" feature
The “live-status” feature is another nice feature that we have already talked about in cisco nso which allows us to run any “show” or “clear” commands in network devices through cisco nso.
The next two scripts are exactly to run “show” and “clear” commands through the python maagic “live-status” function.
This is the script to run „show ip interface brief“ command in a network device.
import ncs with ncs.maapi.single_read_trans("admin", "python") as t: root = ncs.maagic.get_root(t) device_object = root.devices.device["R1"] #print(dir(device_object)) #print(dir(device_object.live_status)) #print(dir(device_object.live_status.ios_stats__exec)) input1 = device_object.live_status.ios_stats__exec.show.get_input() input1.args = ["ip interface brief"] #input1.args = ["cdp neighbor detail"] output = device_object.live_status.ios_stats__exec.show(input1).result print(output) #ios_stats__exec #cisco_ios_xr_stats__exec #nx_stats__exec
The first part of the script is like always to get root access to cisco nso and connect to the network device. For „show“ and „clear“ commands we need only read transnation.
The method „live_status“ is what we need to run in device object to get „live-status“ feature in python code.
Then, depending on the device type, we have different options to be able to run any exec command on a remote device. For example, we use “ios_stats__exec” to run an exec command on iOS devices.
It’s very easy to extract these methods via the “dir” option. I have already extracted for iOS, iOS XR and NX OS devices which are in order „ios_stats__exec“, „cisco_ios_xr_stats__exec“ and „nx_stats__exec“.
Them to run „show“ commands in the remote devices, we always use „show.get_input()“ to get the show command itself as an argument. The exact command without „show“ keyword will be given as the argument.
In our example, we give „ip interface brief“ command as the argument instead of „show ip interface brief“.
Finally „show“ methods and the given argument will be used to run exec command in the remote device.
The „result“ option will be used to get the result of the show commands.
Here we use „print“ command to display the reult on the screen.
Let’s run the python script and see the result of „show ip interafce brief“ command.
majid@majid-ubuntu:~/devnet/cisco_nso/12.cisco_nso_python_api$ python3 6.show_commands.py
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 192.168.2.91 YES NVRAM up up
GigabitEthernet2 unassigned YES NVRAM administratively down down
GigabitEthernet3 unassigned YES NVRAM administratively down down
Loopback100 4.5.6.7 YES NVRAM up up
Loopback123 12.13.14.16 YES NVRAM up up
Loopback135 1.3.5.7 YES NVRAM up up
Loopback150 1.1.1.1 YES NVRAM up up
R1#
clear counters through python maagic "live-status" feature
Finally, we run a “clear” command on a remote device using the „live-status“ function in the python script.
import ncs with ncs.maapi.single_read_trans("admin", "python") as t: root = ncs.maagic.get_root(t) device_object = root.devices.device["R1"] input1 = device_object.live_status.ios_stats__exec.clear.get_input() input1.args = ["cdp counters"] output = device_object.live_status.ios_stats__exec.clear(input1).result print(output)
This is exactly like „show“ command but we use „clear.get_input()“ instead of „show.get_input()“ method to get clear command as an argument.
The command will be given as argument but without „clear“ command exactly like „show“ command.
And the result of clear command will be diplayed using „result“ method.
Result
These are some of the most important scripts using python „maagic“ liberary to show or change network device configuration through cisco nso.
But there are many other options that can be run this library.
You can find some other examples from this link.