Interaction with Cisco IOS XE API
5.3.c i Via NETCONF/YANG using Python ncclient library
General information on “Cisco IOS XE API via NETCONF/YANG”:
- The current config can be retrieved via manager.get_config(<datastore>)
- A config snippet/template can be easily created:
- Retrieve current configuration
- Cut out needed configuration part
- Create XML file or variable and paste the cut out configuration part in between two brackets just like this:
<config>
XML-FORMATTED CONFIG GOES HERE
</config>
- The output of a retrieved config can be easily filtered:
- Create XML file or variable with the desired XML filter
- Apply filter to the retrievial of the configuration
## Basic ncclient library subtree output filter (example)
subtree_filter="""
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<router>
<ospf xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf">
</ospf>
</router>
</native>
"""
## Retrieve and filter running configuration
running_config = netconf_session.get_config("running", filter=("subtree", subtree_filter))
Basic IOS XE OSPF configuration using a template via Python w/ ncclient library (example):
## IMPORT manager FROM ncclient MODULE
from ncclient import manager
## DEFINE PARAMETERS FOR THE NETCONF SESSION
netconf_host = 'ios-xe-mgmt-latest.cisco.com'
netconf_port = 10000
netconf_user = 'developer'
netconf_pass = 'C1sco12345'
## CONNECT NETCONF SESSION
netconf_connection = manager.connect(
host=netconf_host,
port=netconf_port,
username=netconf_user,
password=netconf_pass,
hostkey_verify=False
)
## CHECK IF CONNECT WAS SUCCESSFUL
if netconf_connection.connected == True:
print('NETCONF session to ' + netconf_host + ' established!')
## MAIN PROGRAM
config_edit = open("netconf_ospf_template.xml")
push_config = netconf_session.edit_config(xml, target="running")
## DISCONNECT NETCONF SESSON
netconf_connection.close_session()
## CHECK IF DISCONNECT WAS SUCCESSFUL
if netconf_connection.connected == False:
print('NETCONF session to ' + netconf_host + ' disconnected!')
5.3.c ii Via RESTCONF/YANG using Python requests library and Postman
Basic IOS XE interface configuration via Python w/ requests library (example):
## IMPORT requests AND json MODULE
import requests
import json
## DEFINE THE PAYLOAD DATA TO BE SENT
payload_raw = {
"ietf-interfaces:interface": {
"name": "Loopback1337",
"description": "Configured by PYTHON w/ REQUESTS",
"type": "iana-if-type:softwareLoopback",
"enabled": True,
"ietf-ip:ipv4": {
"address": [
{
"ip": "1.3.3.7",
"netmask": "255.255.255.255"
}
]
}
}
}
## SERIALIZE THE PAYLOAD DATA
payload_json = json.dumps(payload_raw, indent=4)
## DEFINE PARAMETERS FOR THE HTTP REQUEST
url = 'https://ios-xe-mgmt-latest.cisco.com:9443/restconf/data/ietf-interfaces:interfaces'
auth_user = 'developer'
auth_pass = 'C1sco12345'
headers = {
'Accept': 'application/yang-data+json',
'Content-Type': 'application/yang-data+json'
}
## THE FINAL HTTP REQUEST
restconf = requests.post(url,
auth=(auth_user, auth_pass),
verify=False,
headers=headers,
data=payload_json)