Introduction
Within this article we will look at how to interact with a networking device (Cisco CSR) using the NETCONF protocol. To do so, we will use the Python module – ncclient.
What is NETCONF
First of all, What is NETCONF?
NETCONF (NETwork CONFiguration) is a protocol defined by the IETF to “install, manipulate, and delete the configuration of network devices”.
– www.tail-f.com
Further details can be found within our previous article – An Introduction to NETCONF/YANG.
Dependencies
First of all we will need to install some Python dependencies,
pip install ncclient pyang pyangbind xmltodict
Connect
Now that we have the required packages installed we can connect to the device. Like so,
from ncclient import manager m = manager.connect(host='10.29.1.133', port=830, username='cisco', password='cisco', device_params={'name': 'csr'}) print m.connected ... True
Get Capabilities
In order to obtain the devices capabilities we can use the method server_capabilities.
for c in m.server_capabilities: print c ... urn:ietf:params:xml:ns:yang:smiv2:SNMP-FRAMEWORK-MIB?module=SNMP-FRAMEWORK-MIB&revision=2002-10-14 urn:ietf:params:xml:ns:yang:cisco-policy-target?module=cisco-policy-target&revision=2016-03-30 urn:ietf:params:xml:ns:yang:smiv2:CISCO-CBP-TARGET-TC-MIB?module=CISCO-CBP-TARGET-TC-MIB&revision=2006-03-24 urn:ietf:params:netconf:capability:notification:1.0
Get Configuration
To fetch the running configuration of the device you can simply run,
running_config = m.get_config('running')
This will provide us with the variable running_config containing the configuration within an XML data type. Lets pretty print the XML,
import xmltodict
import xml.dom.minidom
print(xml.dom.minidom.parseString(str(running_config)).toprettyxml())
...
<rpc-reply message-id="urn:uuid:01290b52-a324-454b-b754-f8513b0d3b0d" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<version>16.5</version>
<boot-start-marker></boot-start-marker>
<boot-end-marker></boot-end-marker>
<service>
<timestamps>
<debug>
<datetime>
<msec></msec>
</datetime>
</debug>
<log>
<datetime>
<msec></msec>
</datetime>
</log>
</timestamps>
</service>
<platform>
<console xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-platform">
<output>serial</output>
</console>
</platform>
<hostname>csr1000v-1</hostname>
<enable>
<password>
<secret>cisco</secret>
</password>
...
JSON
In addition we can also interact with the configuration by parsing our running_config variable into a dict().
conf_json = xmltodict.parse(str(running_config)) conf_json['rpc-reply']["data"]["native"]["hostname"] ... u'csr1000v'
Close Session
Finally, once all of the necessary operations have been completed we close the session.
m.close_session() ... <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:ebb48b66-185e-4 7ac-9820-73418ed0790f" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><ok></ok></rpc-reply>
- How to Configure a BIND Server on Ubuntu - March 15, 2018
- What is a BGP Confederation? - March 6, 2018
- Cisco – What is BGP ORF (Outbound Route Filtering)? - March 5, 2018
Want to become a networking expert?
Here is our hand-picked selection of the best courses you can find online:
Cisco CCNA 200-301 Certification Gold Bootcamp
Complete Cyber Security Course – Network Security
Internet Security Deep Dive course
Python Pro Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial