forked from zapodeanu/BRKSDN-2935-Barcelona-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetconf_in_action.py
53 lines (38 loc) · 1.9 KB
/
netconf_in_action.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
#!/usr/bin/env python3
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
import xml
import xml.dom.minidom
import urllib3
from ncclient import manager
from requests.auth import HTTPBasicAuth # for Basic Auth
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
from config import RO_HOST, PASS, PORT, USER
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: int_ip_add: the interface IPv4 address
"""
with manager.connect(host=RO_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>
</interface>
</interfaces-state>
</filter>
'''
result = m.get(interface_state_filter)
xml_doc = xml.dom.minidom.parseString(result.xml)
return xml_doc
# get the Interface GigabitEthernet1 operational data
xml_info = get_netconf_int_oper_status('GigabitEthernet1')
print(xml_info.toprettyxml())