Let’s look at a very simple example below of how we can use Python Programming to log into multiple devices and send configs to them.
My lab is simulated on GNS3.
In my lab, I have set up the cloud (My physical PC) which I use as the management PC to configure my devices and 4 routers which we will log into and push some simple configs
- Configure your PC(Cloud) with an IP in your management network.
- Do the same for your devices. Test to ensure that there is connectivity between the PC and the devices.
- Configure SSH and a local login database on the routers plus add ssh on the vty lines. Configure your PC(Cloud) with an IP in your management network.
- Login on ssh to all your devices from your management machine to ensure that they are reachable you have exported the keys.
config t hostname R1 enable password class username cisco priv 15 password cisco ip domain name labingoverload.com crypto key generate rsa modulus 1024 line vty 0 4 login local transport input all int fa1/1 ip add 192.168.99.1 255.255.255.0 description Management interface no shut
Once you have configured the base configs and confirmed that all your devices are reachable from your management PC we can now proceed to writing the actual script.
In this example we will stick to using telnet
Let’s break the code down section by section.
This section imports two python libraries into your code. Both of these come default with Python so there is no need to add them. Libraries contain various elements eg. variables and functions which you can then call into your code. They are useful as they make your work very easy as you dont have to write the code for the elements yourself.
This section enables you to define your own variables that you will be using within your code. Keep an eye out for the variable names and see how we will be using them. The input() function present in Python allows you to request for data from your user. The getpass() function from the getpass library does the same but for the password. It enables you to key in the password without it being displayed on the screen
This section creates a variable called ips. This variable is assigned the contents of a file c:\\myrouters.txt .The open() function is a Python function that is used to access files within your machine. In our case our file contains the IPs of our 4 routers. As you add IPs to this file, ensure you have no spaces between the contents or at the end of the file as Python will read this as well and experience an error. Contents of myrouters.txt are as below
192.168.99.1 192.168.99.2 192.168.99.3 192.168.99.4
Here we create a loop. The purpose of this loop will be to iterate through the contents of ips (which holds the 4 IPs). Then for each iteration execute all the instructions in the loop. If you look closely at the second image, you can see all the indented text within this loop.
We have created a variable count. Count starts of with a content of 0 which is an integer(int). But at every iteration we add 1 to its current value. That means that on the second iteration the value of count will be 2 and so on and so forth.
We are then able to build another variable routerid by using the value we have in count. Within the first iteration the results will be 126.96.36.199. and the second iteration what we will get is 188.8.131.52. Note that we are converting our (int) literals to (str) literals
Now lets discuss the bytes() and .encode(). Both of this enable you to format the data you are sending to the router into a format that the router can understand. For this section i have left the .encode() parameterless and allowed it to work with its default paramater of ascii. There are various other encode formats available but the two most popular ones are utf-8 and ascii.
Let’s go back to the details we read off our file. The strip() function removes any spaces that are present after the last character, we then assign the first IP to a variable named HOST.
Create a variable tn which will be assigned the output of running a function Telnet() which is present within the telnetlib library on our current HOST value. We will use this variable to make other function calls
This section processes the username and password. The script waits until it sees the word Username: This is the normal prompt displayed by the router when you attempt to log in. It then takes contents of our previously defined variable user from the fourth image where the user had been asked to input their username. In our case when prompted we will key in cisco as the username. Again it waits until it sees the word Password: Again normal prompt from a router, It then takes your password (cisco) and sends them to the router. Notice again all data is encoded into ascii.
Finally, we add our configs to the script. We are using routerid which within the first iteration for R1 will be 184.108.40.206. Configuring fa0/0 our IP should be 172.16.1.1 again for the first iteration the value of count is 1.
We then build a loop within a loop. This loop iterates through a range between 100 and 110-1 and for each of those iterations it configures a loopback interface and assigns it an IP. Remember this is happening for every single one of our 4 routers.
Once your code runs successfully you can run some show commands on your routers and confirm the configs.
Experiment with this and add as many more configs to the script as you need.