Scripts that log in for you: what you will learn and the big picture
What you will learn in this module. You will learn to make a small program log in to routers over SSH, read their output, change their configuration and prove that the change worked. You will use Python and a netmiko-style library called nkmiko. By the end you will back up three routers, push OSPF to them from one script, verify the result and read an error message without panic. These are the device-automation skills behind the CCNP Automation exam (350-901 AUTOCOR), and the same skills are reused in the Python for Network Engineers course.
Prerequisites. You need to know what a router, an IP address and the CLI are. You do not need to know how to code. Every line of code in this module is explained, and each chapter teaches one small idea.
Analogy: a very fast, very careful junior engineer
Imagine you give a junior engineer a checklist: "log in to each router, run show ip interface brief, paste the answer into a file." The junior follows the list exactly, never gets bored and never mistypes. A script is that checklist, written in a language the computer understands. The computer is the junior: fast and obedient, but it does exactly what you wrote, not what you meant.
That is both the power and the danger. A script can fix 300 routers in a minute. A wrong script can also break 300 routers in a minute. So we build scripts in small safe steps, and we always check the result.
The lab: one automation host (AUTO) reaches three routers over the management network 10.99.0.0/24.
The lab you will use
The AUTO host is a small Linux-like computer with Python. Three routers R1, R2 and R3 sit on the management network 10.99.0.0/24. A management network is a network used only to administer devices. AUTO logs in to the routers using the username netops and the password NK@2026. These are lab values; in a real network a password never lives inside a script, and Chapter 9 shows what to do instead.
The big idea: collect, change, verify
Every network script is collect, change, verify. Verify is the step beginners skip.
- Collect: log in and read the current state, for example
show ip interface brief. - Change: send configuration lines, only when needed.
- Verify: read the state again and test that the change really worked: the neighbour is up, the interface has the address, the route exists.
A script that prints "done" has only proved that it sent text. It has not proved that the network works.
Your first five minutes on the AUTO host
A script is a text file with the ending .py. You create and run it from the AUTO terminal using these small tools:
lslists the files,cat file.pyshows one, andnl file.pyshows it with line numbers.edit file.py 3 TEXTreplaces line 3 with TEXT. Spaces after the number are kept, which matters in Python.write file.pylets you type a whole file; finish by typingEOFon a line by itself.python3 file.pyruns the script.
Try the smallest script possible. It has two lines: print() is a function (a named action) that shows text on the screen.
print("Hello, network!")
print("2 + 3 =", 2 + 3)
AUTO> python3 hello.py Hello, network! 2 + 3 = 5
Python ran the file from top to bottom, one line at a time. The second line shows that Python can also calculate: 2 + 3 became 5.
Worked example. Without a script: log in to R1, R2, R3, type show ip interface brief on each, copy three screens to a text file. About ten minutes of clicking and one chance in twenty of a slip. With a script: the same job is eight lines of code and takes two seconds, and it can be run again tomorrow, the same way. You will write exactly this script in Chapter 5.
Common beginner mistake. Thinking a script is magic or "smart". It is not. It sends exactly the text you wrote. If you wrote the wrong IP address, it will happily configure the wrong router.
Exam trap. The exam may ask which step makes automation safe. The answer is verification (and testing before production), not speed. Also remember that SSH with a library such as netmiko automates the CLI text, while REST APIs (the next two modules) automate structured data.
The nightly backup that nobody did
A retail company had 40 branch routers. The rule said "back up the configs every night", but it was a manual job and was skipped whenever people were busy. After a power failure two routers came back with empty configs and there was no recent copy. The team wrote a 12-line script that logged in to every router and saved the running config to a dated file. It ran from a scheduler at 02:00. The next failure was a 20-minute recovery.
Lesson: automation is first of all about boring, repeatable jobs done the same way every time.
"Why would you automate with SSH when APIs exist?"
A strong answer: many devices in a real network only have a CLI, so SSH-based automation reaches everything, including old gear. APIs return structured data and are cleaner when they exist. A good engineer knows both and picks per device. Mention collect, change, verify, and that you never push a change you cannot verify.
Key takeaways
- A script is a checklist a computer follows exactly, fast and without typos.
- Every network script follows collect, change, verify.
- "Done" printed by a script proves nothing; reading the state again does.
- In the lab, AUTO (10.99.0.100) reaches R1, R2 and R3 on 10.99.0.0/24 with user netops.
- You run a Python file with
python3 file.pyand read the output from top to bottom.