-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr-create-query-get-config-hostname.py
48 lines (38 loc) · 1.38 KB
/
csr-create-query-get-config-hostname.py
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
42
43
44
45
46
47
48
import sys
import subprocess
from ncclient import manager
if len(sys.argv) < 2:
print("Error - Incorrect arguments")
print('Usage: python3 csr-create-query-get-config-hostname.py <container_name>')
print('Example: python3 csr-create-query-get-config-hostname.py clab-telemetry-testbed-r1')
exit(1)
else:
container_name = sys.argv[1]
check_container = subprocess.getoutput("docker ps -a | awk '{print $NF}' | grep " + container_name)
if check_container != container_name:
print("Error - Incorrect arguments: You need to specify the container name of the network device.")
print('Usage: python3 csr-create-query-get-config-hostname.py <container_name>')
print('Example: python3 csr-create-query-get-config-hostname.py clab-telemetry-testbed-r1')
exit(1)
r = {
"host": container_name,
"port": 830,
"username": "admin",
"password": "admin",
"hostkey_verify": False,
"device_params": {"name": "csr"}
}
session = manager.connect(**r)
print ("\nSession ID: ", session.session_id)
# Create a configuration filter
hostname_filter = """
<native
xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<hostname></hostname>
</native>
"""
# Execute the get-config RPC
reply = session.get_config(source="running", filter=("subtree", hostname_filter))
print("\nHostname of network device "+ container_name + ": \n")
print(reply)
session.close_session()