Cisco Network Automation (API/SDN) Scripts
Write Python scripts for API-driven Cisco network automation. Follow these standards:
Supported APIs
RESTCONF (IOS-XE)
- Base URL:
https://{device}/restconf/data/
- YANG models:
Cisco-IOS-XE-native, ietf-interfaces, ietf-routing
- Use for: interface config, routing, ACLs, system settings
- Content-Type:
application/yang-data+json
NETCONF (IOS-XE / NX-OS)
- Port 830, SSH subsystem
- Use
ncclient library
- Operations:
get, get-config, edit-config, commit
- Filter with XPATH or subtree XML
DNA Center
- Token auth:
POST /dna/system/api/v1/auth/token
- Device inventory:
GET /dna/intent/api/v1/network-device
- Command runner:
POST /dna/intent/api/v1/network-device-poller/cli/read-request
- Template deployment, path trace, client health
Meraki Dashboard API
- Base URL:
https://api.meraki.com/api/v1/
- API key via header:
X-Cisco-Meraki-API-Key
- Organizations, networks, devices, SSIDs, VLANs
Libraries to Use
requests for REST APIs (RESTCONF, DNA Center, Meraki)
ncclient for NETCONF
xmltodict for XML parsing
meraki (official SDK) for Meraki
dnacentersdk for DNA Center
yang-suite references for YANG model exploration
urllib3 (disable warnings for lab self-signed certs)
Script Patterns
RESTCONF Example Structure
import requests
import urllib3
urllib3.disable_warnings()
BASE_URL = "https://{host}/restconf/data"
HEADERS = {
"Accept": "application/yang-data+json",
"Content-Type": "application/yang-data+json"
}
def get_interfaces(host, auth):
url = f"https://{host}/restconf/data/ietf-interfaces:interfaces"
resp = requests.get(url, headers=HEADERS, auth=auth, verify=False)
resp.raise_for_status()
return resp.json()
NETCONF Example Structure
from ncclient import manager
def get_running_config(host, username, password):
with manager.connect(
host=host, port=830,
username=username, password=password,
hostkey_verify=False
) as m:
config = m.get_config(source="running")
return config.xml
Best Practices
- Always handle rate limiting (especially Meraki: 10 req/sec)
- Use session objects for connection reuse
- Implement retry logic with exponential backoff
- Verify API call success with status codes
- Support
--dry-run flag that shows payload without sending
- For lab environments, disable TLS verification with
verify=False but log a warning
- For production, use proper certificate validation
Security Requirements
- NEVER hardcode API keys, tokens, or passwords
- Use environment variables or
.env files (gitignored)
- Rotate tokens appropriately (DNA Center tokens expire)
- Log API calls but mask credentials in output
1---2name: cisco-netauto3description: Generate Python scripts for Cisco network automation using RESTCONF, NETCONF, DNA Center, or Meraki APIs. Use when the user wants to automate Cisco devices using APIs and SDN controllers rather than CLI/SSH.4---56## Cisco Network Automation (API/SDN) Scripts78Write Python scripts for API-driven Cisco network automation. Follow these standards:910### Supported APIs1112#### RESTCONF (IOS-XE)13- Base URL: `https://{device}/restconf/data/`14- YANG models: `Cisco-IOS-XE-native`, `ietf-interfaces`, `ietf-routing`15- Use for: interface config, routing, ACLs, system settings16- Content-Type: `application/yang-data+json`1718#### NETCONF (IOS-XE / NX-OS)19- Port 830, SSH subsystem20- Use `ncclient` library21- Operations: `get`, `get-config`, `edit-config`, `commit`22- Filter with XPATH or subtree XML2324#### DNA Center25- Token auth: `POST /dna/system/api/v1/auth/token`26- Device inventory: `GET /dna/intent/api/v1/network-device`27- Command runner: `POST /dna/intent/api/v1/network-device-poller/cli/read-request`28- Template deployment, path trace, client health2930#### Meraki Dashboard API31- Base URL: `https://api.meraki.com/api/v1/`32- API key via header: `X-Cisco-Meraki-API-Key`33- Organizations, networks, devices, SSIDs, VLANs3435### Libraries to Use36- `requests` for REST APIs (RESTCONF, DNA Center, Meraki)37- `ncclient` for NETCONF38- `xmltodict` for XML parsing39- `meraki` (official SDK) for Meraki40- `dnacentersdk` for DNA Center41- `yang-suite` references for YANG model exploration42- `urllib3` (disable warnings for lab self-signed certs)4344### Script Patterns4546#### RESTCONF Example Structure47```python48import requests49import urllib350urllib3.disable_warnings()5152BASE_URL = "https://{host}/restconf/data"53HEADERS = {54 "Accept": "application/yang-data+json",55 "Content-Type": "application/yang-data+json"56}5758def get_interfaces(host, auth):59 url = f"https://{host}/restconf/data/ietf-interfaces:interfaces"60 resp = requests.get(url, headers=HEADERS, auth=auth, verify=False)61 resp.raise_for_status()62 return resp.json()63```6465#### NETCONF Example Structure66```python67from ncclient import manager6869def get_running_config(host, username, password):70 with manager.connect(71 host=host, port=830,72 username=username, password=password,73 hostkey_verify=False74 ) as m:75 config = m.get_config(source="running")76 return config.xml77```7879### Best Practices80- Always handle rate limiting (especially Meraki: 10 req/sec)81- Use session objects for connection reuse82- Implement retry logic with exponential backoff83- Verify API call success with status codes84- Support `--dry-run` flag that shows payload without sending85- For lab environments, disable TLS verification with `verify=False` but log a warning86- For production, use proper certificate validation8788### Security Requirements89- NEVER hardcode API keys, tokens, or passwords90- Use environment variables or `.env` files (gitignored)91- Rotate tokens appropriately (DNA Center tokens expire)92- Log API calls but mask credentials in output