forked from zapodeanu/BRKSDN-2935-Barcelona-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetconf_restconf.py
78 lines (61 loc) · 2.78 KB
/
netconf_restconf.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
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
import requests
import ncclient
import xml
import xml.dom.minidom
import urllib3
import utils
from ncclient import manager
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
from requests.auth import HTTPBasicAuth # for Basic Auth
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
SW_HOST = '10.93.130.21'
RO_HOST = '10.93.130.41'
PORT = 830
USER ='cisco'
PASS ='cisco'
ROUTER_AUTH = HTTPBasicAuth(USER, PASS)
def get_netconf_int_oper_status(interface):
"""
This function will retrieve the IPv4 address configured on the interface via NETCONF
:param interface: interface name
:return: oper_status: the interface operational status
"""
with manager.connect(host=SW_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
# IOS-XE 16.6.2+ YANG model called "ietf-interfaces"
interface_state_filter = '''
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>''' + interface + '''</name>
<oper-status/>
</interface>
</interfaces-state>
</filter>
'''
result = m.get(interface_state_filter)
xml_doc = xml.dom.minidom.parseString(result.xml)
int_info = xml_doc.getElementsByTagName('oper-status')
try:
oper_status = int_info[0].firstChild.nodeValue
except:
oper_status = 'unknown'
return oper_status
def get_restconf_int_oper_status(interface):
"""
This function will retrieve the IPv4 address configured on the interface via NETCONF
:param interface: interface name
:return: oper_status: the interface IPv4 operational status
"""
url = 'https://' + RO_HOST + '/restconf/data/interfaces-state/interface=' + interface
header = {'Content-type': 'application/yang-data+json', 'accept': 'application/yang-data+json'}
response = requests.get(url, headers=header, verify=False, auth=ROUTER_AUTH)
interface_info = response.json()
oper_data = interface_info['ietf-interfaces:interface']
oper_status = oper_data['oper-status']
return oper_status