BLOGPOST#150 Cisco API Save Config using REST CONF: Learn How to Use RestConf Protocol to Save Your Cisco Device Configurations

Introduction:

Cisco is one of the most popular networking companies in the world. If you work in the IT field, it is likely that you have encountered Cisco routers and switches. Cisco devices are managed through a Command Line Interface (CLI), which allows network engineers to configure them using a series of commands. Saving the configuration of a Cisco device is crucial because it allows engineers to restore the device to a previous state in case of a failure or to replicate the same configuration on another device.

In this tutorial, we will show you how to save the configuration of a Cisco device using the RestConf protocol. The response protocol is a method used to interact with Cisco devices through their web interface. We will demonstrate how to do this using Postman, a popular API development tool, and Python.

Saving the Configuration Using Postman:
To save the configuration of a Cisco device using Postman, follow these simple steps:

-Open Postman and create a new request by clicking on the New button.
-In the request field, select POST as the HTTP method.
-Enter the URL https://<device IP address>/devices/runningConfig in the request field, replacing <device IP address> with the IP address of your Cisco device.
-Under the Authorization tab, select the type of authorization you want to use. In this example, we will be using Basic Auth.
-Enter your username and password in the respective fields.
-Click on the ‘Send’ button to send the request.

If the request is successful, you should see a message that says “Save running config successful.” This means that the configuration has been saved successfully.

Saving the Configuration Using Python:
To save the configuration of a Cisco device using Python, follow these steps:

-Open your Python IDE and create a new script.
-Import the requests module by typing import requests at the beginning of your script.
-Define a variable called url and set it equal to https://<device IP address>/devices/runningConfig, replacing <device IP address> with the IP address of your Cisco device.
-Define a dictionary called headers and set the Authorization field to your authorization credentials. 
-Define a variable called response and set it equal to requests.
-Print the content of the response.

Here is the code:

import json
import requests
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

url = "https://192.168.0.63/restconf/data/Cisco-IOS-XE-native:native/interface/Loopback=1002"


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

response = requests.request("POST", url, headers=headers, verify=False)

print(response.text)
print(response.status_code)
print(response.reason)

If the request is successful, you should see the same message as in the Postman example: “See running config successful.”

Conclusion:
In conclusion, saving the configuration of a Cisco device is a simple yet crucial task for network engineers. By following the steps outlined in this tutorial, you can easily save the configuration of your Cisco devices using the response protocol, either through Postman or Python.

Sample Script for Reference:

import json
import requests
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

url = "https://192.168.0.63/restconf/operations/cisco-ia:save-config/"


headers = {
      'Authorization': 'Basic YWRtaW46YWRtaW4='
}

response = requests.request("POST", url, headers=headers, verify=False)

print(response.text)
print(response.status_code)