Network Automation & Python
Automate network tasks with Python and APIs
Lesson 1: Why Automation?
Network automation replaces manual CLI-based configuration and management with programmatic approaches using scripts, APIs, and orchestration tools, fundamentally transforming network operations. Understanding automation drivers, benefits, and use cases is essential for CCNA certification and modern network engineering. The complexity and scale of modern networks make manual management increasingly impractical—automation is no longer optional but essential for competitive, reliable, and efficient network operations. Traditional manual CLI configuration involves administrators logging into devices individually, typing commands, and verifying results. This approach works for small networks but has critical limitations: speed (configuring hundreds of devices manually takes hours or days versus seconds or minutes with automation), human error (typos, wrong commands, inconsistent configurations plague manual processes—studies show 50-80% of outages result from human error), lack of scalability (one administrator managing thousands of devices manually is impossible), inconsistent results (different administrators may configure devices differently even following documentation), no systematic documentation (configurations exist in device memories and maybe text files, not version-controlled), difficult troubleshooting (finding configuration inconsistencies across hundreds of devices manually is time-consuming), and compliance challenges (verifying security baselines manually across large environments is impractical). Automation addresses these limitations delivering transformative benefits: Speed improvements are dramatic—scripts configure hundreds of devices in minutes versus hours/days manually. Mass changes (updating NTP servers, adding VLANs, modifying ACLs) complete rapidly. Consistency eliminates human error as scripts execute identically every time—no typos, no wrong commands, no forgotten steps. Templates ensure all devices receive identical baseline configurations. Scalability enables small teams managing massive infrastructures—one administrator can manage thousands of devices with automation versus dozens manually. Documentation becomes automatic as automation code serves as executable documentation. Version control (Git) tracks all configuration changes with author, timestamp, and reason, providing complete audit trail. Troubleshooting improves through automated compliance checking comparing current state against desired state and identifying deviations instantly. Backup and recovery automate through scheduled scripts backing up all configurations nightly. Disaster recovery becomes push-button restoration from known-good configurations. Use cases demonstrate automation value: Configuration deployment pushes standard configurations to new devices in minutes—VLAN databases, routing protocol configurations, ACLs, QoS policies all deploy from templates. Configuration backup runs automated nightly scripts backing up all device configurations to centralized repository with version control. Monitoring automation collects metrics (CPU, memory, interface status, errors) from all devices regularly, comparing against baselines and alerting on anomalies. Compliance checking verifies devices meet security baselines (password policies, unused ports disabled, logging enabled) and remediate automatically. Troubleshooting automation gathers diagnostic information (routing tables, ARP caches, interface stats) from multiple devices simultaneously, correlating data identifying problems faster than manual collection. Network validation runs automated tests after changes verifying expected behavior (can we ping the default gateway? Are routing protocol adjacencies up? Is VLAN connectivity working?). Automation tools span the spectrum: Python (programming language providing flexible scripting for custom automation), Ansible (agentless orchestration tool using YAML playbooks for configuration management and automation), Puppet and Chef (configuration management platforms with agents), Cisco DNA Center (Cisco's intent-based networking platform providing GUI-driven automation), Meraki Dashboard (cloud-managed networking with API), Terraform (infrastructure-as-code managing network infrastructure as declarative configurations), and Git (version control for automation code and configurations). Skills requirements shift from pure CLI expertise toward programming fundamentals (understanding variables, loops, conditionals, functions), API interaction (REST APIs, NETCONF, RESTCONF), data formats (JSON, YAML, XML), version control (Git workflows), and troubleshooting scripts (debugging Python, interpreting error messages). Network knowledge remains critical—automation requires understanding what you're automating. The automation journey typically starts with simple scripts (backing up configurations, gathering show command output), progresses to templated configuration deployment, then advances to full infrastructure-as-code with testing pipelines. Organizations need not automate everything immediately—incremental automation of repetitive tasks provides immediate value while building skills. Understanding automation fundamentals enables beginning the transformation from manual to programmatic network operations.
Lesson 2: Python Basics for Networking
Python has become the de facto programming language for network automation due to its readability, extensive libraries, and networking-focused modules. Understanding Python fundamentals and networking libraries is essential for CCNA certification and implementing network automation. While comprehensive Python programming exceeds CCNA scope, understanding basic concepts and key networking libraries enables effective automation script usage and modification. Python networking libraries provide pre-built functionality for common tasks: Netmiko is the most popular Python library for network device automation, providing multi-vendor SSH support (Cisco IOS, NX-OS, Arista, Juniper, HP, etc.), methods for sending commands and receiving output, automatic detection of device prompts, configuration mode handling, and error detection. Netmiko abstracts SSH complexities, letting you focus on automation logic. Paramiko is a lower-level SSH library that Netmiko builds upon, providing more control but requiring more code for basic tasks—use Netmiko unless you need Paramiko's advanced capabilities. NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) provides vendor-neutral APIs abstracting differences between network operating systems. NAPALM provides consistent methods for configuration management, retrieving device facts, and rollback capabilities working identically across Cisco, Juniper, Arista, etc. The Requests library makes HTTP/HTTPS API calls to REST APIs, providing methods for GET, POST, PUT, DELETE operations and handling JSON responses—essential for interacting with modern network controllers and cloud services. Basic Python workflow for network automation involves importing libraries, connecting to devices, sending commands, parsing output, and taking actions based on results. Example workflow: Import Netmiko's ConnectHandler, define device dictionary containing device IP, username, password, and device type, establish connection using ConnectHandler, send commands to get output, parse output (extract specific values), make decisions based on output, optionally send configuration commands, and disconnect. Sample Python script connecting to a router and retrieving version information: 'from netmiko import ConnectHandler' imports the library. Define device: 'device = {"device_type": "cisco_ios", "ip": "192.168.1.1", "username": "admin", "password": "password"}'. Connect: 'net_connect = ConnectHandler(**device)'. Send command: 'output = net_connect.send_command("show version")'. The output variable now contains the command result as a string. You could print it, search for specific text, or parse structured data. Disconnect: 'net_connect.disconnect()'. Sending configuration commands uses 'send_config_set()' method accepting list of commands: 'config_commands = ["interface GigabitEthernet0/1", "description Automated Config", "no shutdown"]', 'output = net_connect.send_config_set(config_commands)'. Netmiko automatically enters configuration mode, sends each command, and exits config mode. Parsing unstructured output (text-based show commands) requires text processing. Python's string methods help: 'if "up" in output:' checks if "up" appears in output. Regular expressions provide powerful pattern matching for extracting specific values from show commands. Libraries like TextFSM parse structured data from CLI output transforming text tables into Python dictionaries or lists. Structured output uses 'send_command()' with TextFSM templates or device commands that output JSON. Some platforms support JSON output: 'output = net_connect.send_command("show version | json")' on NX-OS returns JSON instead of text, easily parsed with Python's json library: 'import json', 'data = json.loads(output)', 'version = data["version"]'. Iterating over multiple devices uses Python loops: Create list of device dictionaries, iterate with 'for device in devices:', connect to each, perform tasks, handle errors gracefully (try/except blocks catching connection failures or authentication errors). Error handling prevents script crashes: 'try:' block contains code that might fail, 'except:' block handles errors, and proper error handling logs failures and continues with remaining devices instead of crashing entirely. Best practices include never hardcoding passwords (use environment variables or prompt for credentials), implementing error handling, logging script actions and results, testing scripts in lab before production, starting with read-only operations before attempting configuration changes, and validating results after configuration changes. Understanding Python networking libraries enables creating effective automation scripts for configuration, monitoring, and troubleshooting tasks.
Lesson 3: REST APIs
REST (Representational State Transfer) APIs provide programmatic interfaces for interacting with network devices, controllers, and cloud services using standard HTTP methods and structured data formats. Understanding REST API concepts, methods, data formats, and authentication is essential for CCNA certification and leveraging modern network programmability. REST APIs enable automation at scale, supporting intent-based networking, cloud integrations, and DevOps workflows impossible with CLI-only approaches. REST fundamentals define an architectural style for building web services emphasizing stateless client-server communication using standard HTTP. RESTful APIs use HTTP methods (GET, POST, PUT, DELETE) to operate on resources (network objects like devices, interfaces, VLANs, ACLs) identified by URLs. APIs return data in structured formats (JSON or XML) enabling programmatic parsing. REST's stateless nature means each request contains all information needed for processing—no session state maintains between requests, enabling scalability and reliability. HTTP methods define operations: GET retrieves data without modifying state (safe and idempotent—multiple identical requests have same effect as single request). Example: GET /api/devices returns list of devices, GET /api/devices/12345 returns specific device. POST creates new resources, sending data in request body. Example: POST /api/devices with JSON body defining new device creates it. POST is neither safe nor idempotent (multiple requests create multiple resources). PUT updates existing resources, replacing entire resource with provided data (idempotent—multiple identical requests produce same result). Example: PUT /api/devices/12345 with updated JSON replaces device configuration. PATCH partially updates resources, modifying only specified fields. DELETE removes resources. Example: DELETE /api/devices/12345 removes device. Understanding appropriate method for each operation prevents unintended modifications. Data formats structure API payloads: JSON (JavaScript Object Notation) is the predominant format for modern APIs. JSON represents data as key-value pairs, arrays, and nested objects using human-readable text. Example: '{"hostname": "router1", "ip": "192.168.1.1", "interfaces": [{"name": "Gi0/1", "status": "up"}]}'. JSON is lightweight, easy to parse, and universally supported. XML (eXtensible Markup Language) uses tagged markup similar to HTML. XML is more verbose than JSON but provides schema validation and is common in legacy systems and NETCONF. Modern APIs predominantly use JSON. HTTP status codes indicate request outcomes: 2xx codes indicate success (200 OK for successful GET, 201 Created for successful POST, 204 No Content for successful DELETE). 4xx codes indicate client errors (400 Bad Request for invalid data, 401 Unauthorized for missing/invalid authentication, 404 Not Found for non-existent resource, 429 Too Many Requests for rate limiting). 5xx codes indicate server errors (500 Internal Server Error for server problems, 503 Service Unavailable for temporary outages). Checking status codes enables error handling in automation scripts. API authentication secures access: Token-based authentication provides bearer tokens (generated through initial authentication) included in subsequent request headers: 'Authorization: Bearer abc123xyz'. Tokens expire after time periods requiring renewal. Basic authentication sends username/password with each request (base64-encoded)—simple but less secure. API keys use unique keys identifying applications, often combined with tokens. OAuth provides delegated authorization for third-party access. Modern APIs use token-based authentication for security and scalability. Python Requests library simplifies API interaction: 'import requests' loads library. GET request: 'response = requests.get(url, headers=headers)' where url is API endpoint and headers contains authentication. Check status: 'if response.status_code == 200:'. Parse JSON: 'data = response.json()' converts JSON response to Python dictionary. POST request: 'response = requests.post(url, headers=headers, json=payload)' where payload is Python dictionary auto-converted to JSON. Example retrieving device list from Cisco DNA Center: Define variables: 'url = "https://dnac.example.com/api/v1/network-device"', set headers with token: 'headers = {"X-Auth-Token": "your-token-here"}'. Make request: 'response = requests.get(url, headers=headers, verify=False)' (verify=False disables certificate validation—use cautiously). Check result: 'if response.status_code == 200: devices = response.json() for device in devices["response"]: print(device["hostname"])'. REST API documentation (usually available via web portals) describes: available endpoints, required/optional parameters, authentication methods, request/response formats, error codes, and rate limits. Major vendors provide API documentation for their platforms—Cisco DNA Center, Meraki, ACI, etc. Best practices include checking status codes, implementing error handling, respecting rate limits, using authentication tokens securely, validating input data, testing in dev environments before production, and versioning API calls to ensure compatibility as APIs evolve. Understanding REST APIs enables leveraging modern network platform programmability for automation and integration.
Lesson 4: NETCONF and RESTCONF
NETCONF and RESTCONF represent model-driven programmability approaches using structured data models (YANG) to configure and monitor network devices, providing standardized interfaces superior to screen-scraping CLI output. Understanding NETCONF, RESTCONF, and YANG is essential for CCNA certification and implementing modern network programmability. These technologies enable reliable automation through standardized data models, transaction support, and comprehensive error handling impossible with CLI-based approaches. Model-driven programmability uses YANG (Yet Another Next Generation) data models defining device configuration and operational state structure. YANG models specify: available configuration parameters, data types and constraints, hierarchical relationships, and valid values. YANG provides structured schemas enabling validation before configuration deployment—invalid configurations reject before reaching devices. YANG models are vendor-neutral standards (IETF, OpenConfig) or vendor-specific (Cisco, Juniper native models). Data models ensure consistency across vendors and enable building generic tools working with any YANG-compliant device. NETCONF (Network Configuration Protocol) is XML-based protocol for network management using SSH transport (TCP port 830). NETCONF provides structured operations: get retrieves operational state data (similar to show commands but structured XML), get-config retrieves configuration data from specific datastores (running, candidate, startup), edit-config modifies configuration in target datastore, copy-config copies entire configuration between datastores, delete-config deletes configuration datastore, lock/unlock provides exclusive configuration access preventing conflicting changes, and commit applies candidate configuration to running (on devices supporting candidate datastore). NETCONF advantages include transaction support with rollback on errors (partial changes don't apply if later commands fail—all-or-nothing atomicity), structured data eliminating CLI parsing, validation against YANG models catching errors before deployment, and separation of configuration and operational data. NETCONF uses XML for encoding: requests and responses are XML formatted based on YANG models. Example get-config request retrieves interface configuration as structured XML easily parsed programmatically. Python ncclient library simplifies NETCONF: 'from ncclient import manager', connect to device: 'with manager.connect(host="192.168.1.1", port=830, username="admin", password="password", hostkey_verify=False) as m:', send get-config: 'config = m.get_config(source="running").data_xml', and parse XML response. RESTCONF provides HTTP-based alternative to NETCONF using same YANG models but RESTful APIs instead of XML-RPC. RESTCONF uses standard HTTP methods (GET, POST, PUT, PATCH, DELETE) for operations, JSON or XML encoding (JSON predominates), HTTPS transport (TCP 443 typically), and URLs identifying resources based on YANG model paths. RESTCONF advantages include: familiar REST API patterns, lighter weight than NETCONF (less overhead), easier integration with web applications and tools, and wider developer familiarity with REST versus XML-RPC. RESTCONF example retrieving interface configuration: 'GET https://device.example.com/restconf/data/ietf-interfaces:interfaces' returns JSON representing all interfaces per the ietf-interfaces YANG model. Modifying configuration: 'PATCH https://device.example.com/restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet1' with JSON body updates specific interface. YANG models organize as hierarchies: modules group related configuration, containers group related leaves, leaves are actual configuration parameters, lists represent repeating elements (interfaces, VLANs). Example ietf-interfaces YANG model: interfaces container contains interface list, each interface has name leaf, type leaf, enabled leaf, etc. Structured hierarchy enables precise configuration targeting specific elements. Model-driven benefits include reliability through validation (invalid configurations caught before deployment), consistency (same models work across vendors), rollback support (failed transactions automatically revert), structured data (no CLI parsing fragility), and comprehensive error reporting (specific parameter and reason for failures). Use cases include configuration management at scale (deploying YANG-based configs to thousands of devices reliably), network orchestration (orchestration platforms using NETCONF/RESTCONF for device interaction), monitoring and telemetry (streaming operational data in structured formats), and multi-vendor environments (standard models enabling vendor-neutral automation). Adoption considerations: not all devices support NETCONF/RESTCONF (newer platforms typically do, legacy may not), learning curve (understanding YANG models and XML/JSON), and tooling (requires different tools than CLI-based automation). However, benefits for large-scale reliable automation are substantial. Platforms supporting model-driven programmability include Cisco IOS XE, IOS XR, NX-OS, Juniper Junos, and Arista EOS. Understanding NETCONF and RESTCONF enables leveraging modern network programmability for reliable, scalable automation.
Lesson 5: Configuration Management
Infrastructure as Code (IaC) treats network configurations as software code managed through version control, automated testing, and continuous deployment pipelines. Understanding IaC principles, tools, and workflows is essential for CCNA certification and implementing modern network operations. IaC transforms network management from manual processes to automated, repeatable, version-controlled workflows improving reliability, speed, and collaboration. IaC principles include declarative configuration (defining desired end state rather than procedural steps—Ansible playbook declares "interface Gi0/1 should have IP 192.168.1.1" versus CLI steps entering config mode), version control (all configurations stored in Git providing complete history, change tracking, and rollback), testing (automated validation before deployment), and repeatability (identical configurations deploy consistently). IaC enables treating networks like software development with established practices for code review, testing, and deployment. Ansible is the most popular IaC tool for network automation providing: agentless architecture (no software installation on managed devices—uses SSH), YAML playbooks (human-readable configuration files defining tasks), modules for network devices (ios_command, ios_config, nxos_command supporting Cisco and other vendors), idempotency (running playbooks multiple times produces same result—commands only execute if needed), and vast community and modules. Ansible workflow: Inventory file lists managed devices with connection details (IP addresses, credentials, device types). Playbooks define automation tasks in YAML: hosts specify target devices, tasks list operations, and modules perform actual work (ios_config pushes configs, ios_command runs show commands). Example playbook configuring VLAN: '---', 'hosts: routers', 'tasks:', '- name: Configure VLAN 10', 'ios_config:', 'lines:', '- vlan 10', '- name Engineering'. Running playbook: 'ansible-playbook -i inventory playbook.yml' executes tasks against inventory devices. Ansible modules abstract vendor differences: ios_config for IOS, nxos_config for NX-OS, eos_config for Arista, but similar syntax. Variables enable reusable playbooks: define VLANs in variables file, reference in playbook, deploy same playbook to multiple sites with different variables. Templates use Jinja2 generating device-specific configs from templates plus variables: template defines structure, variables provide specifics, resulting in customized configs for each device. Version control with Git tracks all changes: every playbook, template, and inventory file commits to Git with author, timestamp, and commit message explaining why. This provides: complete audit trail (who changed what when), rollback capability (revert to any previous version), collaboration (multiple engineers work on same codebase with merge/conflict resolution), and branching (test changes in branches before merging to main). Git workflow: clone repository, create feature branch, make changes, commit with descriptive messages, push to remote repository, create pull request for review, merge after approval. CI/CD pipelines automate testing and deployment: Continuous Integration tests changes automatically when pushed (syntax validation, linting, unit tests), Continuous Deployment automatically deploys approved changes to production. Pipeline example: developer commits playbook changes, automated pipeline runs syntax check, executes playbook against lab devices, runs validation tests, and if all pass, deploys to production with approval. Jenkins, GitLab CI, GitHub Actions provide CI/CD platforms. Testing includes syntax validation (YAML linting), dry runs (--check flag previewing changes without applying), lab deployment (testing in non-production), smoke tests (basic connectivity validation post-deployment), and comprehensive validation (verifying all expected behaviors). Testing catches errors before production deployment. Network-as-Code trends include treating networks like software projects, applying software development best practices (code review, testing, documentation), collaborating through version control, and automating everything from deployment to validation. Benefits include: reduced human error, faster deployments (minutes versus hours/days), consistent configurations, complete change documentation, easy rollback, and improved collaboration. Other IaC tools include Puppet (agent-based configuration management), Chef (infrastructure automation platform), Terraform (cloud and infrastructure provisioning), and NAPALM (network automation abstraction). Tool selection depends on requirements, existing ecosystems, and team skills. Adoption strategy: start with simple read-only automation (gathering configs, show commands), progress to templated configuration deployment, implement version control, add testing, and eventually achieve full CI/CD. Incremental adoption builds skills while delivering value. Understanding configuration management and IaC enables implementing modern network operations with automation, reliability, and agility impossible through manual processes.