Skip to main content

Using Python to script Cisco routers

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:

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.

Python script files

Above I illustrated the usage of Visual Scripts, but normal Python scripts files can also be created, debugged and deployed much the same way. In this context PGT can be considered a Python script manager that runs the scripts while providing advanced hosting services. When you write or debug the script, the Python Interactive console will help you with context sensitive code-completion and quick watch of variables. The article on PGT website documents the feature fairly detailed.

Why would you use it ?

The question is, why would you use Pretty Good Terminal to host and run Python scripts, when you could simply start a script with Python itself ?  What is the added value or benefit of using it ?
As PGT was originally developed for router/switch mass configuration, it's scripting engine was specifically designed for scripting thousands of devices by mastering Telnet and SSH connections over multiple hops of jump servers and collecting the script results in a sophisticated way. As a .NET based application written in C#, PGT can actually host Python scripts using IronPython. This way you can consider PGT as Python script manager providing advanced services for scripts, such as: 
  • Connection management: PGT will manage all Telnet / SSH connection to devices, even if a device is reachable through multiple jump servers.
  • Script result handling: The simple communication interface between PGT and the Python scripts make it very easy to capture and maintain the script results
  •  Robust logging facility: The built-in LogManager will provide simple and yet thread safe logging services for scripts, even if the same script is run in multiple instances at the same time
  •  Lightweight debugging: The built-in Python Interactive console will provide easy script debugging
  •  Intelligent code editor: The Python Interactive console provides auto code-completion and intelli-sense feature to help you writing or editing Python sctips
  •  Script repository: Python Scripts are assigned an alias name in the repository and PGT scripts (connection lists, if you like) can easily refer to them.

And finally, if you are interested you can obtain the software from http://www.prettygoodterminal.com

Comments

Popular posts from this blog

Python scripting lesson for Juniper switches

Collecting comprehensive switch interface information When working with Juniper EX / QFX switches it is often required to collect comprehensive interface status data. Not any single show command will provide all the details and merging the output of several commands is quite time consuming.  On top of this, it can be tricky to find free ports on switches, because unconfigured ports are simply not shown in many commands and the only way to find out available ports is examining the chassis hardware components. In the following article I would like to show you an easy way of collecting such information quickly by using Pretty Good Termina l.    PGT is completely free for private use so you don’t need to worry about any costs involved. What the script does As part of the standard installation, PGT contains a sample Visual Script designed for Juniper EX /QFX switches to collect switch interface information. The script will issue the following CLI commands and m

Python Crumbs for Cisco routers - Part I

In this series I want to show you how to use Python to solve some common tasks related to Cisco router scripting. Update, 2018.06 : I compiled a short training video that will teach you the basic Python scripting in PGT. If you prefer watching a video over reading the article, this is exactly for you :  PrettyGoodTerminal - Using Python Scripts This tutorial will focus only on - so to say - applied Python language and won't go into details with Python syntax or language elements itself for one very good reason : at the beginning you probably want to have results quickly without reading hundreds of pages about the language. I always affirmed that learning something new is much easier when you get  a grasp on it on a practical way. The most common task I came across is getting some basic information of  a router or switch, like system uptime, version, interface status, etc. Of course, you don't need Python for simple queries, but when you need to do some text process