Domain 1 of 4 · Chapter 4 of 6

Network Automation with Python

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing Designing, Deploying and Managing Network Automation Systems premium course — no separate purchase.

14-day money-back guarantee — no questions asked.

Included in this chapter:

  • Four interfaces to a device: reach versus preference
  • NETCONF with ncclient: candidate, commit, transaction
  • RESTCONF with requests: HTTP plus a YANG media type
  • CLI automation: netmiko on top of paramiko
  • Concurrency with asyncio: overlap the waits
  • Decorators: wrapping behavior around a function
  • Packaging: pyproject.toml, wheels, and a venv
  • What the exam stem looks like

The Python network-automation toolkit

TaskPython toolInterface / protocolReach for it when
Transactional config changencclientNETCONF over SSH (port 830)edits must apply atomically or roll back
Direct config editrequestsRESTCONF over HTTPSone JSON edit to running, no transaction needed
No API on the devicenetmiko (on paramiko)CLI over SSHv2screen-scrape when the feature is not modeled
Scale to a fleetasyncioany async-native transportoverlap hundreds of network waits, cap with a Semaphore
Reuse and ship the codedecorators, packagingn/a (Python tooling)cross-cutting logic + an installable console command

Decision tree

Atomic or reversible change? yes NETCONF (ncclient) candidate + commit no One direct edit over HTTPS? yes RESTCONF (requests) direct to running no Network-OS CLI? (prompts, enable, paging) yes netmiko send_command / send_config_set no · no API paramiko raw SSHv2 channel At fleet scale: asyncio.gather + asyncio.Semaphore(n) and bound every call with a timeout

Cheat sheet

  • ncclient connects to NETCONF on port 830
  • edit_config to candidate then commit is transactional
  • lock/unlock protect the datastore during edits
  • get_config with a subtree filter retrieves modeled data
  • RESTCONF edits running directly; NETCONF has candidate+commit
  • requests sends RESTCONF calls with yang-data headers
  • requests.Session reuses the connection and persists auth
  • raise_for_status surfaces HTTP errors explicitly
  • The json= parameter serializes a dict and sets Content-Type
  • netmiko ConnectHandler screen-scrapes CLI over SSH
  • use_textfsm parses raw show output into structured data
  • paramiko is the low-level SSHv2 library netmiko wraps
  • CLI scraping is a fallback to model-driven interfaces
  • asyncio runs concurrent I/O in one thread via an event loop
  • gather fans out tasks; Semaphore bounds concurrency
  • A blocking call stalls the entire event loop
  • asyncio helps I/O-bound work, not CPU-bound work
  • A decorator wraps a function to add cross-cutting behavior
  • A parameterized decorator is a decorator factory
  • pyproject.toml + wheels package automation code
  • Virtual environments and pinned requirements isolate deps

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. https://datatracker.ietf.org/doc/html/rfc7950
  2. https://datatracker.ietf.org/doc/html/rfc6241
  3. https://datatracker.ietf.org/doc/html/rfc8040
  4. https://developer.cisco.com/
  5. https://docs.python.org/3/library/asyncio.html
  6. https://ncclient.readthedocs.io/en/latest/manager.html
  7. https://datatracker.ietf.org/doc/html/rfc6242
  8. https://developer.cisco.com/docs/ios-xe/
  9. https://requests.readthedocs.io/en/latest/
  10. https://requests.readthedocs.io/en/latest/user/quickstart/
  11. https://requests.readthedocs.io/en/latest/user/advanced/
  12. https://developer.cisco.com/docs/dna-center/
  13. https://datatracker.ietf.org/doc/html/rfc4253
  14. https://datatracker.ietf.org/doc/html/rfc4252
  15. https://datatracker.ietf.org/doc/html/rfc4254
  16. https://www.cisco.com/c/en/us/support/docs/security-vpn/secure-shell-ssh/4145-ssh.html
  17. https://docs.python.org/3/library/asyncio-eventloop.html
  18. https://docs.python.org/3/library/asyncio-task.html
  19. https://docs.python.org/3/library/asyncio-runner.html
  20. https://docs.python.org/3/library/asyncio-sync.html
  21. https://docs.python.org/3/glossary.html
  22. https://docs.python.org/3/library/multiprocessing.html
  23. https://docs.python.org/3/library/functools.html
  24. https://docs.python.org/3/library/importlib.metadata.html
  25. https://docs.python.org/3/library/venv.html
  26. https://docs.python.org/3/installing/index.html