-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr-create-query-edit-config-hostname.py
47 lines (37 loc) · 1.5 KB
/
csr-create-query-edit-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
import sys
import subprocess
from ncclient import manager
from jinja2 import Template
if len(sys.argv) < 3:
print("Error - Incorrect arguments")
print('Usage: python3 csr-create-query-edit-config-hostname.py <container_name> <hostname>')
print('Example: python3 csr-create-query-edit-config-hostname.py clab-telemetry-testbed-r1 r1-ios-xe-csr1000v')
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-edit-config-hostname.py <container_name> <hostname>')
print('Example: python3 csr-create-query-edit-config-hostname.py clab-telemetry-testbed-r1 r1-ios-xe-csr1000v')
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)
# Render our Jinja template
hostname_template = Template(open('./jinja2-templates/hostname-native.xml').read())
hostname_rendered = hostname_template.render(
HOSTNAME = sys.argv[2]
)
# Execute the edit-config RPC
reply = session.edit_config(target="running", config=hostname_rendered)
print("\nEditing hostname of network device "+ container_name + " ...\n")
print(reply)
session.close_session()