IOS XE is Open and Programmable
Embedded into a Cisco DNA network are industry standard, open APIs from organizations such as the IETF and OpenConfig. In this demo, you will learn how to leverage the OpenConfig interfaces to do some configuration on a IOS XE device.
Python
#!/usr/bin/env python
'''
Demo: Retrieve the list of interfaces and details from a router based
on the Open Config YANG Data Model.
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Get the full list of Interfaces based on the Open Config model
netconf_filter = '''
'''
print("\nGetting the Configuration of the Interface\n")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <get> RPC to the device - config and state
# response = m.get(netconf_filter)
# Send a <get-config> RPC to the device
response = m.get_config('running', netconf_filter)
print("Returned NETCONF Data.\n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n")
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:5bae2174-9095-4458-a7a4-d8242ca00a48" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">GigabitEthernet3</name>
<config>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<name>GigabitEthernet3</name>
<enabled>false</enabled>
</config>
<ethernet xmlns="http://openconfig.net/yang/interfaces/ethernet">
<config>
<auto-negotiate>true</auto-negotiate>
</config>
</ethernet>
<routed-vlan xmlns="http://openconfig.net/yang/vlan">
<ipv6 xmlns="http://openconfig.net/yang/interfaces/ip">
<config>
<enabled>false</enabled>
</config>
</ipv6>
</routed-vlan>
</interface>
</interfaces>
</data>
</rpc-reply>
Python
#!/usr/bin/env python
'''
Demo: Retrieve the list of interfaces and details from a router based
on the Open Config YANG Data Model.
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Configure a Network Interface based on the Open Config Model
netconf_config = '''
<config>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>GigabitEthernet3</name>
<routed-vlan xmlns="http://openconfig.net/yang/vlan">
<ipv4 xmlns="http://openconfig.net/yang/interfaces/ip">
<addresses>
<address>
<ip>172.16.13.1</ip>
<config>
<ip>172.16.13.1</ip>
<prefix-length>24</prefix-length>
</config>
</address>
</addresses>
</ipv4>
</routed-vlan>
</interface>
</interface>
</config>
'''
print("\nSetting the Configuration of the Interface:")
print(" Interface Name: GigabitEthernet 3")
print(" Description: External Web Services - Secure")
print(" Enabled: true")
print(" IPv4 Address: 172.16.13.1/24")
print("")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <edit-config> RPC to the device
response = m.edit_config(netconf_config, target='running')
print("Returned NETCONF Message. \n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n")
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:d4232818-762e-4432-a58d-4c5c70a4cb15" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<ok/>
</rpc-reply>
Python
#!/usr/bin/env python
'''
Demo: Retrieve the list of interfaces and details from a router based
on the Open Config YANG Data Model.
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Get the full list of Interfaces based on the Open Config model
netconf_filter = '''
'''
print("\nGetting the Configuration of the Interface\n")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <get> RPC to the device - config and state
# response = m.get(netconf_filter)
# Send a <get-config> RPC to the device
response = m.get_config('running', netconf_filter)
print("Returned NETCONF Data.\n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n")
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:5bae2174-9095-4458-a7a4-d8242ca00a48" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">GigabitEthernet3</name>
<config>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<name>GigabitEthernet3</name>
<enabled>true</enabled>
</config>
<ethernet xmlns="http://openconfig.net/yang/interfaces/ethernet">
<config>
<auto-negotiate>true</auto-negotiate>
</config>
</ethernet>
<routed-vlan xmlns="http://openconfig.net/yang/vlan">
<ipv6 xmlns="http://openconfig.net/yang/interfaces/ip">
<config>
<enabled>false</enabled>
</config>
</ipv6>
</routed-vlan>
</interface>
</interfaces>
</data>
</rpc-reply>
Access Cisco Innovation through APIs
Cisco networks include many innovative technologies that are made available through native YANG models and interfaces such as NETCONF and RESTconf. In this demo, you can learn to programmatically add a new virtual network with the "Cisco-IOS-XE" data models for VRF, BGP and interfaces.
Python
#!/usr/bin/env python
'''
Demo: Retrieve the details of a standard VRF Configuration
using the Cisco IOS XE Data Models
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Get the full list of Interfaces based on the Open Config model
netconf_filter =
print("\nGetting the Configuration from the router.\n")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <get> RPC to the device - config and state
# response = m.get(netconf_filter)
# Send a <get-config> RPC to the device
response = m.get_config('running', netconf_filter)
print("Returned NETCONF Data.\n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n"))
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:fa4fae2c-5eda-4e17-823c-e7cacaaa9c40" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<router>
<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-bgp">
<id>200</id>
<bgp>
<log-neighbor-changes/>
</bgp>
<address-family>
<no-vrf>
<ipv4>
<af-name>unicast</af-name>
</ipv4>
</no-vrf>
</address-family>
</bgp>
</router>
</native>
</data>
</rpc-reply>
Python
#!/usr/bin/env python
'''
Demo: Configure a new standard VRF Configuration
using the Cisco IOS XE Data Models
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Configure a
netconf_config = '''
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<ip>
<vrf>
<name></name>
<rd>65000:3</rd>
<route-target>
<direction>export</direction>
<target>65000:3</target>
</route-target>
<route-target>
<direction>import</direction>
<target>65000:3</target>
</route-target>
</vrf>
</ip>
<interface>
<GigabitEthernet>
<name>2.3</name>
<encapsulation>
<dot1Q>
<vlan-id>13</vlan-id>
</dot1Q>
</encapsulation>
<ip>
<address>
<primary>
<address>192.168.13.2</address>
<mask>255.255.255.252</mask>
</primary>
</address>
</ip>
</GigabitEthernet>
<Loopback>
<name>13</name>
<ip>
<vrf>
<forwarding>
<word></word>
</forwarding>
</vrf>
<address>
<primary>
<address>192.168.120.3</address>
<mask>255.255.255.255</mask>
</primary>
</address>
</ip>
</Loopback>
</interface>
<router>
<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-bgp">
<id>200</id>
<neighbor>
<id>192.168.13.1</id>
<remote-as>100</remote-as>
</neighbor>
<address-family>
<with-vrf>
<ipv4>
<af-name>unicast</af-name>
<vrf>
<name></name>
<neighbor>
<id>192.168.13.1</id>
<remote-as>100</remote-as>
<activate/>
</neighbor>
<redistribute>
<connected/>
</redistribute>
</vrf>
</ipv4>
</with-vrf>
</address-family>
</bgp>
</router>
</native>
</config>
'''
print("\nAdding a new VRF Configuration:")
print(" Peering Interface: GigabitEthernet2.3")
print(" IP: 192.168.13.2")
print(" BGP Neighbor: 192.168.13.1")
print("")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <edit-config> RPC to the device
response = m.edit_config(netconf_config, target='running')
print("Returned NETCONF Message. \n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n")
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:d4232818-762e-4432-a58d-4c5c70a4cb15" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<ok/>
</rpc-reply>
Python
#!/usr/bin/env python
'''
Demo: Retrieve the details of a standard VRF Configuration
using the Cisco IOS XE Data Models
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
# Get the full list of Interfaces based on the Open Config model
netconf_filter =
print("\nGetting the Configuration from the router.\n")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <get> RPC to the device - config and state
# response = m.get(netconf_filter)
# Send a <get-config> RPC to the device
response = m.get_config('running', netconf_filter)
print("Returned NETCONF Data.\n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n"))
Response
<?xml version="1.0" ?>
<rpc-reply message-id="urn:uuid:db8d516a-9e39-4ca3-a273-7f077856302e" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<ip>
<vrf>
<name>VRF-DEMO3</name>
<rd>65000:3</rd>
<route-target>
<direction>export</direction>
<target>65000:3</target>
</route-target>
<route-target>
<direction>import</direction>
<target>65000:3</target>
</route-target>
</vrf>
</ip>
<interface>
<GigabitEthernet>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">2.3</name>
<encapsulation>
<dot1Q>
<vlan-id>13</vlan-id>
</dot1Q>
</encapsulation>
<ip>
<vrf>
<forwarding>
<word>VRF-DEMO3</word>
</forwarding>
</vrf>
<address>
<primary>
<address>192.168.13.2</address>
<mask>255.255.255.252</mask>
</primary>
</address>
</ip>
</GigabitEthernet>
<Loopback>
<name>13</name>
<ip>
<vrf>
<forwarding>
<word>VRF-DEMO3</word>
</forwarding>
</vrf>
<address>
<primary>
<address>192.168.120.3</address>
<mask>255.255.255.255</mask>
</primary>
</address>
</ip>
</Loopback>
</interface>
<router>
<bgp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-bgp">
<id>200</id>
<bgp>
<log-neighbor-changes/>
</bgp>
<neighbor>
<id>192.168.13.1</id>
<remote-as>100</remote-as>
</neighbor>
<address-family>
<with-vrf>
<ipv4>
<af-name>unicast</af-name>
<vrf>
<name>VRF-DEMO3</name>
<neighbor>
<id>192.168.13.1</id>
<remote-as>100</remote-as>
<activate/>
</neighbor>
<redistribute>
<connected/>
</redistribute>
</vrf>
</ipv4>
</with-vrf>
<no-vrf>
<ipv4>
<af-name>unicast</af-name>
<neighbor>
<id>192.168.13.1</id>
<activate/>
</neighbor>
</ipv4>
</no-vrf>
</address-family>
</bgp>
</router>
</native>
</data>
</rpc-reply>
The Network Joins the Discussion with ChatOps
See how application developers can leverage the power of the network by building applications and integrating them with network technologies such as Embedded Event Manager (EEM). Build applications that react in realtime to network events and analytics data.
Python
#!/usr/bin/env python
'''
Demo: Trigger a notification about config change.
'''
from ncclient import manager
import xml.dom.minidom
# Import device details: HOST, PORT, USER, PASS
from credentials import *
from random import randint
# Get a random integer to use
rint = randint(1,100)
# Configure a Network Interface based on the Open Config Model
netconf_config = '''
<config>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>GigabitEthernet1</name>
</interface>
</interface>
</config>
'''.format(rint)
print("\nChanging the Description of the Management Interface")
print("")
# Connect to the device with NETCONF
with manager.connect(host=HOST, port=PORT, username=USER,
password=PASS, hostkey_verify=False,
device_params={'name': 'default'},
allow_agent=False, look_for_keys=False) as m:
# Send a <edit-config> RPC to the device
response = m.edit_config(netconf_config, target='running')
print("Returned NETCONF Message. \n")
# Print out the raw returned XML
print(xml.dom.minidom.parseString(response.xml).toprettyxml())
print("\n")
Network Operations with Cisco Webex Teams
Join DevNet
DevNet helps you get your questions answered so you can keep your project moving.