6.1 Interpret basic Python code used for network automation
ENCOR 350-401 v1.2 · 6.0 Automation and Artificial Intelligence
ENCOR expects you to read short Python scripts that automate network tasks: variables and data types, lists and dictionaries, loops and conditions, functions, and libraries such as requests, ncclient and netmiko. You do not write large programs; you say what a given script does and spot its mistakes.
What you must know
- Read Python types: str, int, bool, list, dict, and nested data from JSON.
- Follow
forloops,if/elif/else, functions and return values. - Recognise common libraries: requests (REST), json, ncclient (NETCONF), netmiko (SSH CLI).
- Read error handling (
try/except) and status code checks.
A typical script
import requests
BASE = "https://catalyst-center.example.com/dna"
def get_token(user, password):
r = requests.post(f"{BASE}/system/api/v1/auth/token", auth=(user, password), timeout=10)
r.raise_for_status()
return r.json()["Token"]
token = get_token("api-ro", "…")
headers = {"X-Auth-Token": token, "Accept": "application/json"}
devices = requests.get(f"{BASE}/intent/api/v1/network-device", headers=headers, timeout=10).json()["response"]
for d in devices:
if d["reachabilityStatus"] != "Reachable":
print(f"{d['hostname']:20} {d['managementIpAddress']:15} UNREACHABLE")get_tokenposts the credentials with basic authentication and returns theTokenfield of the JSON answer;raise_for_status()stops on a 4xx or 5xx.headersis a dictionary; the token goes inX-Auth-Token..json()["response"]is a list of dictionaries, one per device.- The loop prints each device whose
reachabilityStatusis notReachable, aligned in columns with f-strings.
One API call returns the whole inventory (1); the loop does the filtering.
Building blocks to recognise
| Code | Meaning |
|---|---|
vlans = [10, 20, 30] | A list; vlans[0] is 10 |
intf = {"name": "Gi1", "up": True} | A dictionary; intf["name"] is "Gi1" |
for v in vlans: | Loop over each item |
if x == 1: … elif …: … else: … | Conditions |
def f(a, b=2): return a + b | A function with a default argument |
try: … except requests.exceptions.Timeout: … | Handle an error |
json.loads(text) / json.dumps(obj, indent=2) | JSON text to Python data and back |
Commands
python3 script.py | Run a script. |
pip install requests netmiko ncclient | Install the usual network libraries. |
Exam traps
- Indentation defines blocks in Python.
- Lists are indexed from 0; dictionaries are accessed by key.
True/Falsein Python becometrue/falsein JSON.=assigns;==compares.
Check yourself
data = {"interfaces": [{"name": "Gi1"}, {"name": "Gi2"}]}: what isdata["interfaces"][1]["name"]?- "Gi2".
- What does
r.raise_for_status()do? - Raises an exception if the HTTP response is a 4xx or 5xx error.
- Which library would you use to send CLI commands over SSH?
- Netmiko (or Paramiko).
More topics in 6.0 Automation and Artificial Intelligence
- 6.3 Describe what a data modeling language such as YANG offers
- 6.4 Describe the Catalyst Center and SD-WAN Manager APIs
- 6.5 Interpret REST API status codes and response bodies from Catalyst Center and RESTCONF
- 6.6 Construct an EEM applet that automates a task, a troubleshooting step or data collection
- 6.7 Compare agent-based and agentless orchestration tools