Created
November 3, 2019 07:04
-
-
Save afaqk9394/5a652788453a235139a36e1d86f58d6a to your computer and use it in GitHub Desktop.
NETCONF to fetch the router’s hostname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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