Python Libraries for Retrieving Network Device Information using RESTCONF | Cisco Exam 300-535-SPAUTO

Python Libraries for Network Device Information Retrieval

Question

Which two Python libraries are used to write a script to retrieve network device information using RESTCONF? (Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

BE.

The two Python libraries used to write a script to retrieve network device information using RESTCONF are requests and json.

  1. requests - It is a popular Python library used for making HTTP requests to web servers. It provides a simple and elegant way of interacting with web services and APIs. Using requests, you can easily make HTTP requests like GET, POST, PUT, DELETE, etc. to REST APIs.

  2. json - It is a built-in Python library used for encoding and decoding JSON data. JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is commonly used for transmitting data between a server and a web application, as an alternative to XML.

To retrieve network device information using RESTCONF, you would typically use the following steps:

  1. Import the necessary Python libraries - requests and json.
  2. Define the RESTCONF API endpoint URL of the network device you want to retrieve information from.
  3. Use requests library to make an HTTP GET request to the RESTCONF API endpoint URL.
  4. Parse the JSON data returned by the API using the json library.
  5. Extract the required information from the JSON data and store it in a variable or display it on the console.

Example code snippet to retrieve network device information using RESTCONF:

python
import requests import json # Define the RESTCONF API endpoint URL of the network device url = "https://<device-ip>:<port>/restconf/data/<data-path>" # Define headers for the RESTCONF API request headers = { "Accept": "application/yang-data+json", "Content-Type": "application/yang-data+json" } # Define credentials for the network device username = "admin" password = "password" # Use requests library to make an HTTP GET request to the RESTCONF API endpoint URL response = requests.get(url, auth=(username, password), headers=headers, verify=False) # Parse the JSON data returned by the API using the json library data = json.loads(response.text) # Extract the required information from the JSON data and store it in a variable or display it on the console print("Hostname:", data["ietf-system:system-state"]["hostname"]) print("Model:", data["ietf-system:system-state"]["platform"]["model-name"]) print("Software Version:", data["ietf-system:system-state"]["platform"]["software-version"])

In this example, the requests library is used to make an HTTP GET request to the RESTCONF API endpoint URL of a network device, with the required headers and credentials. The json library is then used to parse the JSON data returned by the API, and extract the required information from the JSON data, which is then displayed on the console.