GNS3 Talks: Python for Network Engineers with GNS3 (Part 2) – Configure VLANs on switches.

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 IOSvL2 switch with VLANs by using Python on an Ubuntu Docker container.

Script used in this video is available on GitHub here:…

This is one of multiple videos teaching you network programmability using Python and GNS3. might take on the learning programming or any other technology is to just do it,try it make
mistakes, pick yourself up and try again.

Don’t try and do it perfectly on the first run. Create some code and then iterate or improve and refine your code as you go along. GNS3 makes this process very easy.

In the previous video, I configured the router using a Python script. This is what our Python script looks like. We programmatically configured the loopback addresses and OSPF on the Cisco IOS of the router.

Now let’s configure the switch.

The router was given this IP address through DHCP. What I’ll do is configure the switch manually on the SVI so IP address question mark. We have to specify the IP address here,so we’ll use 72. So hostname switch1.

We need to configure login details, so I’ll set an enable password a Cisco,I’ll set up a username of David, With a password of Cisco and on the vty lines, I’m going to use the command log in local and transport input all.

So let’s manually test that telnet works a telnet to the IP address of the switch. Log in with our username of David, password of cisco enable put password in, we can login, so we’ve proven that telnet works on the switch.

On the switch at the moment, show VLAN brief shows us that only the default VLANs are configured.

Now previously we configured this Python script.

What I’m going to do now is copy this Python script to another script called pythonS1script1

As I said in the previous video, we are not trying to do everything perfectly here we are trying to get started and we’ll iterate and improve as we go along.

So let’s use an nano to edit that script. In this case the IP address of the switch is

We’ll be prompted for our username and our password.

We’re going to telnet to that host. we are looking for a username and password prompt.

So let’s confirm that that’s what we’re going to see. We are prompted for a username so that’s correct. We’re prompted for a password so that’s correct.

So the script will correctly look for and then enter those values. We’re going to send the enable command to the switch enter a password of cisco, then type conft and we’ll configure a VLAN of 2.

Now you can as always refine this and improve it but I’m going to type exit and then create VLAN 3 and then exit and let’s get rid of all of this.

So let’s control K that control U, paste it back control K, control U, control U.

Now you could if you preferred just do this copy that and paste it in. So this creates VLANs 2, 3, 4, 5 6 control X, save the configuration.

Now again before I run the script, notice we only have the default VLANs configured and what I’ll do now is type pythonS1script1 user name is David password is cisco. This is what was configured on the switch. We can see in the output of the switch that it was configured via telnet show VLAN.

Notice the VLANs have been added to the VLAN database. VLAN 2, 3, 4, 5, 6 have been added it was as simple as that to use Python to configure a network switch.

Now what we could do here is an example is set a description.

So Python VLAN2 as an example control X save the script, run it again and hopefully what we should see in the output here show VLAN.

What’s nice about the output of the script is we see that there’s a problem I used the wrong command. So first manually test command is not supported so manually test and then automate. So let’s do that again.

The description is actually the name command save that, run the script again. I didn’t configure that manually it was configured by the scripts to show VLAN, notice the name has changed.

So let’s do that for the other VLANs, control K, control U, control U, control U, control U, control U. This is one of the reasons why you want to use variables and the loops because it saves a lot of time doing this manually but we’ll leave that for a different script.

So python pythonS1script1 user name is david, password is cisco.

We see what was configured and if we type show VLAN we can see the configuration on the switch. This is the script that we created.

Again, it’s not perfect. You can optimize the script, you can improve it. There’s a lot of things that you can do to make it better but this demonstrates how you can quickly start
programming networks using Python with the GNS3.

I hope you enjoyed the video. If it was of benefit to you, please like it and please subscribe to my YouTube channel. I wish you all the very best.

  • David Bombal