-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr-create-query-get-interface-ietf.py
66 lines (54 loc) · 2.7 KB
/
csr-create-query-get-interface-ietf.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
import subprocess
from ncclient import manager
from jinja2 import Template
if len(sys.argv) < 2:
print("Error - Incorrect arguments")
print('Usage: python3 csr-create-query-get-interface.py <container_name> [<interface_name>]')
print('Example for the GigabitEthernet1 interface: python3 csr-create-query-get-interface-ietf.py clab-telemetry-testbed-r1 GigabitEthernet1')
print('Example for all interfaces: python3 csr-create-query-get-interface-ietf.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-interface.py <container_name> [<interface_name>]')
print('Example for the GigabitEthernet1 interface: python3 csr-create-query-get-interface-ietf.py clab-telemetry-testbed-r1 GigabitEthernet1')
print('Example for all interfaces: python3 csr-create-query-get-interface-ietf.py clab-telemetry-testbed-r1')
exit(1)
else:
if len(sys.argv) == 2:
# Render our Jinja templates
interface_config_template = Template(open('./jinja2-templates/interfaces-ietf-config.xml').read())
interface_config_rendered = interface_config_template.render()
interface_state_template = Template(open('./jinja2-templates/interfaces-ietf-state.xml').read())
interface_state_rendered = interface_state_template.render()
elif len(sys.argv) == 3:
# Render our Jinja templates
interface_config_template = Template(open('./jinja2-templates/interface-ietf-config.xml').read())
interface_config_rendered = interface_config_template.render(
INTERFACE_NAME = sys.argv[2]
)
interface_state_template = Template(open('./jinja2-templates/interface-ietf-state.xml').read())
interface_state_rendered = interface_state_template.render(
INTERFACE_NAME = sys.argv[2]
)
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)
# Execute the get RPCs
reply_config = session.get(filter=("subtree", interface_config_rendered))
print("\nInterface configuration of network device "+ container_name + ": \n")
print(reply_config)
reply_state = session.get(filter=("subtree", interface_state_rendered))
print("\nInterface operational status of network device "+ container_name + ": \n")
print(reply_state)
session.close_session()