Learn Python programming with GNS3. In this series of videos I will show you how you can quickly and easily program Cisco networks using Python. In this video we program a Cisco IOSv router using Python on an Ubuntu Docker container.

The script used in this video is available on GitHub here:


This is one of multiple videos discussing Python programming. Now there’s a lot of information on the internet with regards to Python programming, but in these series of videos I’m going to concentrate solely on the use of Python with networking devices and specifically with gns3 .

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 by 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 obtain. 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 the telnet library or telnet client available in Python 2.7. Now there’s a large debate in Python community about which version of Python you should learn. Python 2.7 is being replaced by Python 3. But notice on the Python website you can still download Python 2.7 and a lot of operating systems and network devices have version 2.7 by default. I’m going to show you both Python 2.7 and python 3 as part of these videos or as part of my GNS3 Python course. You may only have Python 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 our GNS3 topology.

In GNS3, I’m going to drag a iOS router to the workspace as well as an iOSv switch. These devices take a while to boot up, so to allow my docker containers to connect to the Internet quickly, I’m going to use a layer 2 switch. One of the docker containers available is in a blue 2 container and that’s what I’m going to use in this topology rather than trying to get python 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 docker container and add that to our GNS3 topology.

Again, my belief is start or 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 get started.

So on the Ubuntu machine I’m going to edit to the config. I’m going to set this device to use DHCP 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 docker container has already booted up and we can already ping google.com even though the switch and the router are still

So how to get update? We will get update our references in ubuntu, that’s done, will install Python.

In this first example I’m going to install Python 2.7. So Python version of Python that we’re using is 2.7. 1. So here is the script that we’re going to start off with it’s not perfect. What we will be doing now is not necessarily the most optimized way of implementing a script like this but the
idea there is, we want to get started, get it working by for instance creating a loop back on the router and configuring VLANs on the switch and then we can build on our script and optimize it.

I’m going to use nano to create a file so let’s call this python Router1 script 1 to start off with. And then I’m going to paste the code off the internet into nano. so we’re importing modules here get passes telnet library.

The device that we’re going to telnet to is going to be the router. So we need to configure the router’s IP address, username and password and other details for when the script telnets
to the router. So before we continue with our script let’s configure the network devices. See here’s the router it’s booted up. Now I’ll bypass the initial configuration dialog.

Transcription limited by YouTube.

  • David Bombal