A year ago or so I posted an article on LinkedIn, How to configure 50000+ CPE routers.
Now I want to go a step further in scripting and share you my way of using Python for scripting Cisco routers. I show you the software and its usage for large scale scripting deployment because I do believe it is an extremely powerful network scripting tool, and all who face similar challenges might find it a good friend. It is not a commercial, I do use the software every day and I develop Pretty Good Terminal as needed to achieve my goals quicker and simpler in a more & more elegant way.
In this article I do not wan to dive deep into scripting details but rather just flash some cool features of PGT worth highlighting. Should you be interested in details, you will find quite detailed descriptions about these features on the website of the software.
A Visual Script using Python
The software I developed for router mass-configuration has evolved a lot since its first publication and now allows the creation of scripts like drawing an active flowchart, called a Visual Script. Both Python or C# can be then chosen as the script language.
This time I was tasked to create a script for updating around 20.000, Cisco 800 series routers. Armed with the aforementioned tool-set, it was easy to transform the requirements to a script flow:
In the flowchart each element have its own Python code to do its humble part of the whole script by dynamically constructing and issuing specific CLI commands, analyze the result, and build an update script accordingly.
For instance, the UpdatePorts element issues the CLI command "sh run int vlan1" and parses the result in order to generate the required IOS commands to transfer the same ip settings of the VLAN1 interface to Vlan10:

For instance, the UpdatePorts element issues the CLI command "sh run int vlan1" and parses the result in order to generate the required IOS commands to transfer the same ip settings of the VLAN1 interface to Vlan10:
All elements have its own scope for local variables, but equally can refer to each other or to variables in the script's global namespace, and can make dynamic decisions in the process flow depending on the value of some attribute.
When all elements finished collecting data and the update script was built it can finally be sent to the device. This is done by the RunUpdateScript element which will construct the CLI command from the UpdateScript list:
Should anything go wrong in the command execution, using the built-in Log manager it is just a one line statement to to log the errors for later investigation:
When the script is tested and works fine, it can be deployed easily to a selected list of routers.
Of course, not any script would work correctly without testing and debugging. For this reason a lightweight Python debugger was also built into PGT. As the script is started and elements get the focus in the execution path, the code will break at the breakpoints set and variable values can be examined or the code can be executed step-by-step:
When all finished, PGT will collect the script result, report it and then proceed to the next device in the list.



Comments
Post a Comment