Cisco RestConf Cheatsheet

Short overview and Cheatsheet for Cisco based Restconf automation.

Home / Posts / Cisco RestConf Cheatsheet

I decided to migrate this blog here, which was based on wordpress for the past 8 years, to hugo. For this reason, all technical information stays here 😁

This post is a work in progress, as i’m currently using Restconf for different customer, I want to save/post some of the infos i discovered. Get a Portchannel Subinterface

Base URL for all the following examples

1
https://{HOST}:{PORT}/restconf/data/

In the beginning i really struggled to find the right models, what can help is the yang representation you can find on github posted by cisco.

An important concept to understand is the split between configuration & operational data (Which makes sense as the devices itself always worked like that). Thankfully the naming of the resource gives a good hint what data you can expect. If there is a -oper in the name, you are querying for operational data.

Example Paths for configuration

Get a Portchannel Subinterface

1
Cisco-IOS-XE-native:native/interface/Port-channel-subinterface/Port-channel=1.600

Delete a Portchannel Subinterface, difference to Get is that you make a DELETE call to the API

1
Cisco-IOS-XE-native:native/interface/Port-channel-subinterface/Port-channel=1.600

Get a Interface/Subinterface, here it’s important to escape the / in the name

1
2
3
4
import requests
INTERFACE = requests.utils.quote("1/0/1.3204", safe='')

Cisco-IOS-XE-native:native/interface/HundredGigE={INTERFACE}'

Get the configuration for a specific vrf

1
Cisco-IOS-XE-native:native/vrf/definition=TEST

Get the bgp configuration

1
Cisco-IOS-XE-native:native/router/bgp

Example Paths for operational data

Operational information about all the vrfs

1
Cisco-IOS-XE-vrf-oper:vrf-oper-data

Operational information about all the interfaces

1
Cisco-IOS-XE-interfaces-oper:interfaces

Operational information for a specific interfaces, here it’s also important again to escape the / in the name

1
2
3
4
import requests
INTERFACE = requests.utils.quote("1/0/1.3204", safe='')

Cisco-IOS-XE-interfaces-oper:interfaces/interface=HundredGigE{INTERFACE}

Python examples

These are really simple examples that can hopefully help you to get on speed (and help me remember how this stuff works) :)

Create a new VRF

This examples creates a new vrf on the defined switch. It only works when the vrf doesn’t already exists.

 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
import requests

requests.packages.urllib3.disable_warnings()

HOST = '1.1.1.1'
PORT = 443
USER = 'user'
PASS = 'password'

def main():
    url = f'https://{HOST}:{PORT}/restconf/data/Cisco-IOS-XE-native:native/vrf'

    headers = {
        'Content-Type': 'application/yang-data+json',
        'Accept': 'application/yang-data+json'
    }

    data = {
        "Cisco-IOS-XE-native:definition": {
            "name": "RESTCONF"
        }
    }

    response = requests.post(
        url,
        auth=(USER, PASS),
        headers=headers,
        verify=False,
        json=data
    )

    print(response)

if __name__ == '__main__':
    main()

Update a VRF

The update process works a bit different, the following example shows how to add an additional rt to the vrf TEST

Example of the vrf before our change

1
2
3
4
5
6
7
8
9
vrf definition TEST
 rd 1:1
 !
 address-family ipv4
  route-target export 1:1
  route-target export 3:3
  route-target import 1:1
  route-target import 2:2
 exit-address-family
 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
import requests

requests.packages.urllib3.disable_warnings()

HOST = '1.1.1.1'
PORT = 443
USER = 'user'
PASS = 'password'

def main():
    url = f'https://{HOST}:{PORT}/restconf/data/Cisco-IOS-XE-native:native'

    headers = {
        'Content-Type': 'application/yang-data+json',
        'Accept': 'application/yang-data+json'
    }

    data = {
        "native":{
            "vrf": {
                "definition": {
                    "name": "TEST",
                    "address-family": {
                        "ipv4": {
                            "route-target": {
                                "import-route-target": {
                                    "without-stitching": [
                                        {
                                            "asn-ip": "5:5"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    response = requests.patch(
        url,
        auth=(USER, PASS),
        headers=headers,
        verify=False,
        json=data
    )

    print(response.text)

if __name__ == '__main__':
    main()

And then the VRF after the change

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Current configuration : 212 bytes
vrf definition TEST
 rd 1:1
 !
 address-family ipv4
  route-target export 1:1
  route-target export 3:3
  route-target import 1:1
  route-target import 2:2
  route-target import 5:5
 exit-address-family

Special Case - No Switchport

For people that work in the network area, the no switchport config should be well known. It changes a switched port to a routed port, on platforms that support it. This is normally done directly on an interface and a simple task. But through restconf, I struggled a bit to find the right configuration and path. First strange thing was, that in no output that I read out from the switches, the no switchport was visible. With a lot of testing i finally found a way to do it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def main():
    INTERFACE = requests.utils.quote("1/0/26", safe='')
    url = f'https://{HOST}:{PORT}/restconf/data/Cisco-IOS-XE-native:native/interface/HundredGigE={INTERFACE}/switchport-conf'

    headers = {
        'Content-Type': 'application/yang-data+json',
        'Accept': 'application/yang-data+json'
    }

    data = {
        "Cisco-IOS-XE-native:switchport-conf": {
            "switchport": False
        }
    }

    response = requests.patch(
        url,
        auth=(USER, PASS),
        headers=headers,
        verify=False,
        json=data
    )
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy