Skip to content

Instantly share code, notes, and snippets.

@afaqk9394
Created November 3, 2019 07:04
Show Gist options
  • Select an option

  • Save afaqk9394/5a652788453a235139a36e1d86f58d6a to your computer and use it in GitHub Desktop.

Select an option

Save afaqk9394/5a652788453a235139a36e1d86f58d6a to your computer and use it in GitHub Desktop.
NETCONF to fetch the router’s hostname
# import the ncclient library
from ncclient import manager
import sys
import xml.dom.minidom
# use the IP address or hostname of your CSR1000V device
HOST = 'ios-xe-mgmt-latest.cisco.com'
# use the NETCONF port for your IOS-XE device
PORT = 10000
# use the user credentials for your IOS-XE device
USER = 'developer'
PASS = 'C1sco12345'
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
hostname_filter = '''
<filter>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<hostname></hostname>
</native>
</filter>
'''
result = m.get_config('running', hostname_filter)
xml_doc = xml.dom.minidom.parseString(result.xml)
hostname = xml_doc.getElementsByTagName("hostname")
print("Hostname: " + hostname[0].firstChild.nodeValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment