GNS3 Talks: Python for Network Engineers with GNS3 (Part 13) - Netmiko, SSH, Python Cisco switches

preview_player
Показать описание

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.

================================
Script on GitHub:
================================
Transcription:

This is one of the multiple videos teaching you network programmability using Python and GNS3.
In the previous video I showed you how to use Netmiko to configure a single IOSv switch in GNS3.

Now we'll extend to the topology and configure three switches. So I've got three switches booted and configured for SSH in GNS3 and I can now configure them through Netmiko. Netmiko makes it easy. This is the configuration for switch 1 here's the configuration for switch 2

Here's the configuration for switch 3. If I have additional switches I would simply configure those additional switches in the Python script and now what I can do is create a variable that contains IOS v layer 2 switch 1 switch 2 and switch 3 and then I can run a loop. So I can say four devices in all devices, in other words these three switches we're going to connect to those devices and then I'm going to run a sub loop where I create 20 VLANs on each of those switches. So it's quite a simple script which I'll now copy.

Here's my Ubuntu host I've got a script called a Netmiko1. So let's create another one called Netmiko 2 and I'll paste that script in.

The script is once again available on github and I've referenced it below this video. So there's the script. Now before I run the script I'll reduce the number of VLANs to 5 so that you don't have to watch me wait for the VLANs to run.

So now python netmiko2 before I run the script, let's have a look at the VLANs on Switch 2 as an example.

show vlan brief switch 2 only has the default VLANs configured
switch 3 sh vlan brief only has the default VLANs configured switch 1 was previously configured.

So it has more VLANs, so sh vlan brief notice it has 30 VLANs configured but what we should notice is when the script SSH is into the switches that it will configure VLANs on switch 1 which is currently doing and then it will connect to switch 2 and switch 3 and configure the 5 VLANs.
So it's now configured the VLANs on switch 1. We’re told that it's creating VLAN to which will be on switch 2. We can see that it's connected to switch 2, so sh vlan brief, we can see that 4 VLANs have been configured on switch 2 and now 5 VLANs have been configured.

sh vlan brief there was a problem accessing switch 3. Notice there's a trace back saying that there's a problem on switch 3.

So let's look at the code and make sure that everything looks good so that all looks the same. Can we ping 192.168.122.73 yes we can but perhaps SSH hasn't been enabled properly on this switch.
So let's 192.168.122.73 notice the connection is refused. So let's ensure that the keys are created.

So crypto key generate RSA.
Let's try and SSH again. Notice we are asked whether we want to login and we can access the switch. So let's run the script again and make sure that the VLANs are configured on the 3 switches. So it's currently configuring switch 1, I’ll speed the video up so said that you don't have to watch it cycle through the VLANs again.

It’s configuring switch 2 at the moment and now its configuring switch 3.
So show vlan brief you can see 4 VLANs configured 5 VLANs have been configured.
So the script has successfully configured 5 VLANs on the three switches. So it was as simple as that to configure 3 switches with the VLANs using a very basic Python script.

Now again these scripts can be optimized but at this point I simply want you to see that it's
possible for you to configure network devices using Python. It may sound really complicated and very difficult but actually it's not.

I hope you enjoyed this video. If you did, please like it and please subscribe to my YouTube channel. I wish you all the very best!
Рекомендации по теме
Комментарии
Автор

simply awesome, u r making me love pyhton and programming.

vishalkanojia
Автор

I'll always likes and appreciate your videos

mohab
Автор

Thank you for sharing your knowledge. You have explained in very clear and logical way which seem the topic very easy.

dinfahim
Автор

this is really coollll sir, than you so much for this valuable knowledge

siddheshnandiwadekar
Автор

how to save config with copy run star??

virtualvnet
Автор

can you tell me how do i assign ip address to the 500 loopback interface.
on a singe router. cause i need script to do my college project.

atharva
Автор

Dear David,
Thank you very much for the great video.
Can you please show me how to configure different loopback interfaces on routers using a loop and a file ? For example, configure loopback (1.1.1.1) on R1, (2.2.2.2) on R2, etc. I was thinking about a scenario where we have an MPLS core where we need to configure loopbacks on the core routers.

blueblink
Автор

David,
I made a comment on this conversation thread about NAPALM (Python network configuration library) and was thinking about this overnight and feel I should apologise somewhat for making various suggestions in comments.
I'm sure you will cover many more things in this series and in your upcoming course.
Is there a better place to make suggestions on things people would like to see covered or are you happy enough with public suggestions in video comments.

I don't know that much about Python basics and my previous experience came out of requirements in getting a script working about 5-6 months ago to accomplish a specific task.
I enjoyed the change and challenge but don't have the underlying Python fundementals yet.

I was able to get to around the current stage your video series is at and made a Paramiko/Netmiko script to connect via SSH to gather information. One of my requirements was not to have username/password in the script and used other libraries to do this.
I had to move on with other things in work and have not really returned to anything useful with Python since then but interest remains in learning some useful Python basics and your series been very interesting.

What would have been my ideal next tasks I would like to have done but found either too difficult or didn't have time carry on with.

* Export Cisco show command results to a .txt, .csv file or possibly email.
* Write basic Python scripts to run tests and export results
* Use other libraries to create ping or SNMP scripts and export results - ping a range of IP's or subnet's.
* Using Linux commands within Python and exporting results - Monitor a directory for new files and email when new file is created or changed as a practical example.

Basically to perform useful tasks and make something practical whilst learning the basic concepts along the way. These things can of course be Googled but would be nice to build an actual understanding along the way.

Like your videos for example where you learn by doing.

Thanks again and hope you find this useful.

alreid
Автор

Dear Bro,
How can i install NAT cloud on GNS3 well. Thanks..

khantphyo
Автор

David,
Great Videos..
Is there anyway to save the ubuntu workstation after all of the software is loaded. If I shut down I have to reload python or whatever package you have added during these videos. Thanks in advance.
Lloyd

prindle