forked from zapodeanu/BRKSDN-2935-Barcelona-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestconf_in_action.py
44 lines (27 loc) · 1.16 KB
/
restconf_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
#!/usr/bin/env python3
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
import json
import requests
import urllib3
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, USER
ROUTER_AUTH = HTTPBasicAuth(USER, PASS)
def pprint(json_data):
"""
Pretty print JSON formatted data
:param json_data:
:return:
"""
print(json.dumps(json_data, indent=4, separators=(' , ', ' : ')))
def get_restconf_int_oper_status(interface):
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']
return oper_data
# get interface operation data using RESTCONF
json_info = get_restconf_int_oper_status('GigabitEthernet1')
pprint(json_info)