Vai al contenuto

Using Python to script Cisco routers + how to connnect 60.000 Routers

https://www.techguruhub.net/

If you want to using Python on your Cisco Router, you definelly need to check this Procedure and how Cisco Router Works, after this assay on the next step you will find how to use python script on your cisco routers!

 

 

Using Python to script Cisco routers + how to connnect 60.000 Routers

 

How To Configure 60000 Routers

Using Python to script Cisco routers + how to connnect 60.000 Routers
how to connnect 60.000 Routers

In this brief essay I want to share an application with you which has been created as a solution to a real life situation.
Recently I have been engaged in projects where I had to setup a very big number of Cisco routers and switches. There are of course strong and robust management systems to handle this task, but in these circumstances their participation were not an option for numerous reasons.

Nevertheless, I had the duty to review the present configuration of the routers, and change them depending on what I discovered. The decision process was pretty sophisticated and included changing ACLs, BGP routing, TACACS servers, banner text and dialer interface attributes depending on router MPLS VPN membership and also certain interface VRF forwarding property.

At first I began to utilize some scriptable telnet client, where I could create a VBS script and have it run from the application.

There was no method to debug the script, since it was hosted by the telnet client program and it supplied no debug capabilities. The only means of debugging was to show message boxes. Not that successful, I must say…
The second difficulty was, that the devices were available only via jump servers, occasionally – indeed very often – through many jump servers. As a consequence, the script had to be further convoluted with login methods both for SSH and telnet.
After a time the script grew incredibly intricate and without true debugger help it was practically difficult to continue this way.

As I had fairly a solid expertise with .Net programming in C# I decided to start creating my own tool. I knew it would be highly time demanding, but I visioned a useful tool and I wanted to accomplish it.

Previously I was working for Telecom firm and my duty was to setup numerous Nexus switches for a freshly established fabric path network. At that time the way I did was to design the configuration script in excel, save it as a csv file and built a simple Pearl script to send the instructions to a specific set of devices. It had shown to be a highly productive manner so I decided to take the same road this time once again, but now using C# and .Net.

My objective was to design an application that is easy to install and simple enough to be used by individuals who are unfamiliar with scripting languages, but still give a manner of expansibility for those who know programming. I mention programming, since I also did not want to re-invent the wheel and find out some new, proprietary scripting language, but utilize C# instead.

The outcome of my effort is called Pretty Good Terminal, and I have been using it for my business for some time now and have successfully setup more than 60000 Cisco routers. I believe this tool it is unique in various aspects. It is unusual since it is targeting a certain demographic that have to cope with such a vast number of devices yet it has a very little footprint. It is also unique, that you may connect in to a distant router beyond numerous jump servers with only one click.

READ: Dual-boot guide: how to install Windows 11 and Windows 10 on the same PC (in the best and easiest way)

The program on its own is capable of connecting in to devices via Telnet or SSH and give them particular instructions in a script, all running in numerous concurrent script windows. And when the need arises for more intricate logic to both verify and based on the findings update a device, particular plug-in modules come in helpful.

A spin-off of the development is a plug-in, which is on one hand a very convenient network discovery tool, while on the other hand displays PGT’s expansibility. It utilizes Cisco CDP protocol to detect neighbour devices on a network and build a Visio drawing from the gathered data, including adding IOS version and interface settings as shape data.

Should you be engaged in network device mass-configuration, I recommend you to check this program out, and you will be astonished how simple it may be to deal with so many routers and switches.

 

1587124213 278 0 Using Python to script Cisco routers + how to connnect 60.000 Routers python

 

Using Python to script Cisco routers

 

https://www.techguruhub.net/

 

I want to take a step farther in scripting and offer you my approach of utilizing Python for programming Cisco routers. I present you the program and its use for wide scale scripting deployment since I do feel it is an extraordinarily strong network scripting tool, and anyone who face similar issues could find it a nice friend. It is not a commercial, I do use the program every day and I build Pretty Good Terminal as required to reach my aims faster and easier in a better & more beautiful manner.


In this essay I do not desire to delve deep into scripting intricacies but rather simply flash some great aspects of PGT worth noting. Should you be interested in specifics, you will find pretty thorough information of these functions on the website of the program.


A Visual Script using Python


The program I built for router mass-configuration has progressed a lot since its original release and now enables the development of scripts like sketching a live flowchart, dubbed a Visual Script. Both Python or C# may be then selected as the script language.


This time I was requested to write a script for upgrading roughly 20.000, Cisco 800 series routers. Armed with the aforementioned tool-set, it was straightforward to turn the requirements to a script flow:

c0b64a1625fc842890f5170efcc2fbe2 Using Python to script Cisco routers + how to connnect 60.000 Routers python

In the flowchart each element have its own Python code to execute its small portion of the total script by dynamically generating and executing specified CLI commands, assess the output, and create an update script appropriately.


For instance, the UpdatePorts element performs the CLI command “sh run int vlan1” and parses the output in order to create the needed IOS instructions to transfer the identical ip settings of the VLAN1 interface to Vlan10:

faustide Using Python to script Cisco routers + how to connnect 60.000 Routers python

All components have its own scope for local variables, but equally may refer to each other or to variables in the script’s global namespace, and can make dynamic choices in the process flow based on the value of some attribute.
When all components completed gathering data and the update script was generated it may finally be delivered to the device. This is done via the RunUpdateScript element which will generate the CLI command from the UpdateScript list.

faustide 1 Using Python to script Cisco routers + how to connnect 60.000 Routers python

Should something go wrong in the command execution, using the built-in Report manager it is simply a one line statement to to log the problems for subsequent investigation:

pankaj patel 561360 unsplash 1024x683 1 Using Python to script Cisco routers + how to connnect 60.000 Routers python

When the script is tested and functions good, it can be deployed simply to a specified list of routers.
Of course, not any script would operate properly without testing and debugging. For this reason a lightweight Python debugger was also included into PGT.

As the script is launched and items receive the attention in the execution path, the code will break at the breakpoints defined and variable values may be studied or the code can be performed step-by-step:

kusto query language kql from scratch v1 Using Python to script Cisco routers + how to connnect 60.000 Routers python

When everything done, PGT will gather the script result, report it and then go to the next device in the list.
Python script files
Above I highlighted the use of Visual Scripts, but standard Python scripts files may also be developed, debugged and deployed in the same manner. In this perspective PGT might be regarded a Python script manager that executes the scripts while offering sophisticated hosting features. When you develop or debug the script, the Python Interactive terminal will aid you with context sensitive code-completion and rapid monitoring of variables. The post on PGT website details the feature very extensively.


Why would you utilize it ?


The point is, why would you utilize Pretty Good Terminal to host and execute Python programs, when you could just start a script using Python itself ? What is the additional value or advantage of utilizing it ?
As PGT was initially built for router/switch mass setup, it’s scripting engine was particularly intended for scripting thousands of devices by mastering Telnet and SSH connections via several hops of jump servers and gathering the script results in a sophisticated fashion. As a .NET based application developed in C#, PGT can really host Python programs using IronPython. This manner you may regard PGT as Python script manager offering enhanced services for scripts, such as:

 

  • Connection management: PGT will handle all Telnet / SSH connection to devices, even if a device is available via numerous jump servers.
  • Script result handling: The simple communication interface between PGT and the Python scripts make it extremely straightforward to record and preserve the script results.
  • Robust logging facility: The built-in LogManager will offer basic and yet thread safe logging services for scripts, even if the same script is performed in several instances at the same time.
  • Lightweight debugging: The built-in Python Interactive terminal will offer simple script debugging
  • Intelligent code editor: The Python Interactive console includes auto code-completion and intelli-sense functionality to enable you write or modifying Python sctips.
  • Script repository: Python Scripts are granted an alias name in the repository and PGT scripts (connection lists, if you prefer) may simply refer to them.