Network programmability made easy

David Bombal
A free video tutorial from David Bombal
CCIE #11023, over 15 years of network training experience
4.6 instructor rating • 43 courses • 649,428 students

Lecture description

c

Learn more from the full course

Python Programming for Network Engineers: Cisco, Netmiko ++

Network Programmability and Network Automation using GNS3 and Python for Cisco engineers: Telnet, SSH, Paramiko, Netmiko

05:55:31 of on-demand video • Updated August 2020

  • Learn network programmability with GNS3 and Python
  • Learn how to automate the configuration of network devices with Python
English [Auto] This is one of the multiple videos discussing Python programming. Now there's a lot of information on the Internet with regards to Python programming but in this series of videos I'm going to concentrate solely on the use of Python with networking devices and specifically with Aji and history. I'm a very strong believer in practical learning. Think about it when you were a child did you go to university or school to learn how to ride a bike. Typically you learnt how to ride a bike riding a bike and falling off a few times making mistakes. But as you practiced you got better and better at riding a bike. I think the same is true for any other skill that you want to gain and any other knowledge that you want to attain. The best way to do it is to just do it. Make mistakes. Pick yourself up and try again. And with that in mind I'm going to show you how quickly you can get programming a Cisco network using Python in Google. I've done a search for Python telnet and my first hitch is that telnet to a library or telnet client available in Python 2.7. Now there's a large debate in the pipeline community about which version of Python you should learn Python 2.7 is being replaced by Python 3 but notice on the python web site you can still download Python 2.7 and a lot of operating systems and network devices have a version 2.7 by default. I'm going to show you both Python 2.7 and Python 3. You may only have one 2.7 available. So it's good to know that but you should also learn Python 3 for the future. So notice the simple script that we can copy from the Python documentation. We'll start off with a script and start programming. Now genius 3 typology ingenius 3. I'm going to drag a Iowas the router to the workspace as well as an Iowa switch. These devices take a while to boot up. So to allow my doctor containers to connect to the Internet quickly I'm going to use a laser to switch one of the darker containers available is in a buntu container. And that's what I'm going to use an apology rather than trying to get points on running on your Windows PC or trying to get Python working on other operating systems. It's going to be a lot quicker for us just to use a dock container and add to that to our genius free typology. Again my belief is start learning as quickly as you can make mistakes and learn from that. Don't get hung up with trying to learn the best way to do everything right away just get started. Some people believe that they need to have a degree to become a programmer or use programming skills in networking. I don't necessarily agree with that just to get started so on the buntu machine I'm going to edit the config I'm going to set the device to use DHP and click save and click start to boot up the network. I'm going to open up a console to each device. Now again the Cisco devices will take a while to boot up but our a container has already booted up and we can already ping Google dot com even though the switch and the Rodda are still being apt get update. Will update all references in a boon to that's done we'll install Python in the first example I'm going to install Python 2.7 points on version of Python that we're using is 2.7 to 12. So here is the script that we going to start off with. It's not perfect. What we doing now is not necessarily the most optimized way of implementing a script like this. But the idea is we want to get started get it working by for instance creating a loop back on the router and configuring the lines on the switch and then we can Bouldon off script and optimize it. I'm going to use Neno to create a file so it's called us Python wrote a one script one to start with and then I'm going to paste the code of the Internet into Nnenna So we are importing modules Sheir get parsed sis Telenet library the device that we going to telnet to is going to be the router so we need to configure the router IP address username and password and other details for when the script telnet to the Rodda. So before we continue with our script let's configure the network devices. So here's the Raw Data booted up now. I'll bypass the initial configuration dialog or configure the router with a user name of one configure and enable password of Cisco. And in this example I'm going to configure a user name of David with a password of Cisco on the Viti y lines. We are going to use log in local. Going to log in with a username and password and I'm going to enable both telnet and S-sh on the gigabit 00 interface. We will configure this interface for DHP and no shut the interface on the router. In the real world you typically going to configure Rodda IP addresses statically. But in this example I'll use DHP just to get started. The router has now been assigned an IP address of 1 and 2 1 6 8 122 71. Let's test the connection to the road manually solve telnet manually to the router from itself and log in to verify and then close down the telnet session. Now this first example we using the telnet to a library and it's going to be looking for certain strings and based on those strings it's going to send output to the router. So it's important that you test manually first before you try and do this with a script. So the host that we're going to telnet to is 1 2 1 6 8 1 22 seventy one. In other words the IP address that was allocated here through DHC P So Python makes programming a lot easier because she can leverage the work that others have done. So we are importing various modules rather than trying to write our own low level telnet library. We're just going to leverage another python telnet to library and other options such as get postes. So we don't have to worry about setting up complicated password information. Basically the user will be asked for a password. So let's change that to something like into your telnet username that's going to be stored in a variable which is called a later in the script. We're going to be prompted for a password that's going to be stored in a variable called password and that's going to be passed to the rodded late in the script so we're going to tell that to the host which is this favorable which we have manually configured. Now again using the concept of get started and iterate all improve. Once you've done a bit of coding we are hard coding a lot of values in the script programmers with lots of knowledge will tell you you can do this better. You can do that better and so forth and so one we're not going to worry about that in the same way that a s.c.i knows BGP better than a CCMA does a program that was 10 years or 20 years of programming knowledge is going to know programming better than you if you've just started. But we're not going to worry about that. We simply are going to get started and then we'll improve as we go along. So what it's looking for here is a logon prompt. Now this is where you need to be careful when we telnet to the router. It's not going to display logging it's going to display user name. So what we want to do is change that to user name. We're looking for the prompt user name. And then we again pass the variable the user to the router and do a carriage return than if a password is configured. We are going to look for the password prompt so let's confirm that that's correct. Yes it is. We will be asked for our password. So the scripts going to look for that and then it's going to pass the password that we entered to the router and press carriage return. Now this is incorrect. This is a linux command. We don't want to use that. What I'm going to do now is press control K which is catch control you. Allows me to paste so paste a few lines here. So what we want to do is send the Kamandi Nabl to the router and then we want to send an enable Ponsford of Cisco. And then we want to do a confetti and then we want to type interface loop back at 0 control. Copy that paste it back then we want to do the IP address one hundred one with a mask of that control. K copy's control you pace back. I'll paste it down here as well. Then we are going to type and to go back to privileged mode control you to pace back. And then we get a top exit to exit out of the router. Now we are not doing any validation here. Programmers with lots of experience will tell you you need to validate stuff. We'll get to that later. We just want to do some basic programming no control X will allow us to Xscape a nano yes to save the script. A script is saved as Python or one script one. Now on the Rodda exit or the telnet session show IP interface brief. There is no loopback configured on the road at the moment. What I want to do is do a debug telnet so we can see where the telnet works. In other words we want to see the script telnetting to the Rodda. And what I'm going to do now is type python Python or one script one we've prompted for a user name which in my case is David postes who does Cisco and notice something happened. We see output on the rodder we can see that it was configured from Konsole by David on Viti Why notice suddenly we see a loopback interface coming up in the output of our telnet script. We see the output of what took place on the router but notice on the router show IP interface brief. We now see a loopback configured not just to prove the show version shows us that we're working on the router show IP interface brief shows us that the loopback was configured as a show run interface loop back at zero. There's the loop back on the router. This output was displayed because in the script and in this case all carot we've got this command right at the end saying print Telenet Riedl said was as simple as that to get a Python script to do something on a router ingenius three. Let's do one more. Let's say we want to configure a novel loopback address so all copy that control Caig control you control you. Now again those guys who've been programming for a long time will tell you that you need to configure here and that's a better way to do it. But to get started we'll do this manually and save script run the script again before I run the script to gain show on the interface brief. We only have Lubec is zero configured whose name is David passata Cisco. We can see that the router was configured loopback one has come up show IP interface brief. There is loopback one configured with the IP address of toot toot toot or T. Now again we could do other things as simple as this let's configure OSPF and then we'll look at configuring the switch so paste some lines in here. Talk burrata if one network 0.00 to Sarah with a mosque in area Zerah and I'll get rid of this line control X to escape y to save just that you can see the script. This is all script at the moment. I've added these lines. So let's run the script Python or one script one before I run that show IP protocols. Notice no SPF is enabled on the router use name is David. What is Cisco. That's what we did on the router. We can see it was configured to show IP protocols. Notice OSPF is now running on the router because we configured OSPF through how python script in subsequent videos. I'll show you how to optimize the script and how to improve on it. But it was as simple as that. To use Python to programmatically change configurations on network devices put to the python script below this video if you want to use the script that I created hope you enjoy this video. If you did please like it please subscribe to my YouTube channel. I wish you all the very best.