We can now display the response
data in a human friendly format. However
as it’s structured data it’s very easy for use to manipulate and pull out the
bits that we need

j =
xmltodict.parse(r.xml)['rpc-reply']['data']

print json.dumps(j, indent=2)

Example 2

In this next
example we will execute a configuration level command and also show how we can
work under sub prompts. Assuming we
already have a session object “s” from the previous example:

command = 'configure terminal ; vlan
10 ; name PYTHON_TEST10'

r = s.exec_command({command})

We can see we get some basic output to say the command
completed correctly:

print r

If we try to execute a command with the wrong cli syntax
then we get an error which can be captured in a script easily. To show this then just try the following:

command = 'configure terminal ;
blah blah'

r = s.exec_command({command})

Conclusion

That’s it for this article.
Only two examples, but from those two examples you should be able to do almost
any task that you can do on the Nexus cli.

Note on OpenConfig

Lastly a quick note on OpenConfig. In the last blog post and this one we've looked at using NETCONF to interact with Junos and Nexus devices. For both we've been able to send cli formatted commands because we're using proprietary data models.

This is great for developing quick and easy tools to do simple tasks. However to build complex apps on large networks it's harder to use as we always need to determine what device type we're connecting to. Then we need to structure the configuration data for that device type and we'll get back different structured response data.

OpenConfig means that we can use exactly the same data model on different vendor devices. For example I could send a standard piece of YANG data over NETCONF to configure a BGP parameter and get back response data in a standardised format. IOS-XR has put a huge amount of development into OpenConfig in version 6.0.0 but I've yet to play around with it, so please see the links in the references section below.