Course Content
Spanning Tree
An overview of how switches become aware of other switches and prevent loops.
0/2
Multiple Spanning Tree Protocol (MST)
0/1
Advanced OSPF
The (OSPF) protocol scales well with proper network planning. IP addressing schemes, area segmentation, address summarization, and hardware capabilities for each area should considered when designing a network.
0/6
Introduction to Automation Tools  
To provide a high-level overview of some of the most common configuration management and automation tools that are available.
0/3
ENCOR Course
About Lesson

Basic Python Components and Scripts

llustrates the components of Python scripts and how to interpret them.

  • Python has become one of the most common programming languages in terms of network programmability.
  • This section leverages the new knowledge you have gained in this chapter about APIs, HTTP operations, DevNet, and GitHub.

Interpreting Python Scripts

 

  • Example 28-12 shows a Python script that sets up the environment to log in to the DNA Center sandbox. It uses the same credentials used with the Token API earlier.
  • Sandbox – The line that says ENVIRONMENT IN USE=“sandbox” corresponds to the selection of the sandbox type of lab environments.
  • Express – Back end that is used for the DevNet Express Events that are held globally at various locations and Cisco office locations.
  • Custom – used in the event that there is already a DNA Center installed either in a lab or another facility, and it needs to be accessed using this script.

Interpreting Python Scripts

  • Variables are used to target the DevNet DNA Center sandbox specifically.
  • The variables shown are in the JSON data format that was discussed earlier in this chapter. Remember that JSON uses key/value pairs and is extremely easy to read and interpret.
Variable Value Description
host sandboxdnac.cisco.com Cisco DNA Center sandbox URL
port 443 TCP port to access URL securely (HTTPS)
username devnetuser Username to log in to Cisco DNA Center sandbox (via API or GUI)
password Cisco123! Password to log in to Cisco DNA Center sandbox (via API or GUI)
  • In 28-13, you can see the key/value pair “username”: “devnetuser”. The structure used to hold all the key/value pairs in this script is called a dictionary. In this particular Python script, the dictionary is named dnac. The dictionary named dnac contains multiple key/value pairs, and it starts and ends with curly braces ({})

Interpreting Python Scripts

  • The first section of code tells the Python interpreter what modules this particular script will use.
  • Think of a module as a collection of actions and instructions.

Interpreting Python Scripts

  • Modules help Python understand what it is capable of.
  • If a developer tried to do an HTTP
  • GET request without having the Requests modules imported, it would be difficult for Python to understand how to interpret the HTTP call. Although there are other ways of doing HTTP calls from Python, the Requests modules greatly simplify this process.
  • Example 28-18 shows the second section of the get_dnac_devices.py script.

   

Interpreting Python Scripts

  • Functions are blocks of code that are built to perform specific actions. Functions are very structured in nature and can often be reused later on within a Python script.
  • The print function, which can be used to print data to a terminal screen. You can see the print function at the end of the get_dnac_devices.py script.
  • In order to execute any API calls to Cisco DNA Center, you must be authenticated, using the Token API.

   

  • The script ties the Token API to the Network Device API call to retrieve the information from DNA Center.
  • The line that says header [“x-auth-token”] = token is mapping the JSON response from the previous example, which is the token, into the header called x-auth-token. The URL for the API has changed to network_device, and the response is sending a requests.get to that URL.
  • This is exactly the same example used with Postman earlier in this chapter.

 

 

  • The final section of get_dnac_devices.py shows code that ties the dnac dictionary in the Env_Lab.py script to the dnac_login function covered earlier.
  • In addition, the print function takes the response received from the response.get that was sent to the Network Device API and puts it into the table format that was specified earlier in the script with the name dnac_devices.

 

Join the conversation