This repository was archived by the owner on Aug 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathget_hostname.py
More file actions
41 lines (34 loc) · 1.37 KB
/
get_hostname.py
File metadata and controls
41 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python
# import the ncclient library
from ncclient import manager
import sys
import xml.dom.minidom
# the variables below assume the user is leveraging the
# network programmability lab and accessing csr1000v
# use the IP address or hostname of your CSR1000V device
HOST = '198.18.133.218'
# use the NETCONF port for your CSR1000V device
PORT = 2022
# use the user credentials for your CSR1000V device
USER = 'admin'
PASS = 'C1sco12345'
# create a main() method
def main():
"""Main method that retrieves the hostname from config via NETCONF."""
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:
# XML filter to issue with the get operation
hostname_filter = '''
<filter>
<native xmlns="urn:ios">
<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[0].firstChild.nodeValue)
if __name__ == '__main__':
sys.exit(main())