GNS3 Talks: Python for Network Engineers with GNS3 (Part 4) - Create switch VLANs using loops
David Bombal
Udemy: Get the course for $10 here: https://goo.gl/QYC988 GNS3 Academy: Get the course for $10 here: https://goo.gl/vnZJhg More free Python videos here: https://www.youtube.com/playlist?list=PLhfrWIlLOoKPn7T9FtvbOWX8GxgsFFNwn
Script used in this video is available on GitHub here:
https://github.com/davidbombal/pythonvideos/commit/da3045beca3c1b8766b2c9c92d2cdf55b3b8f7f1
Transcription:
This is one of multiple videos teaching you network programmability using Python and GNS3.
In the previous video we created S1script2.py and I told you that it's inefficient to create VLANs in this way on a switch, now it works.
So as an example, we can use the command S1script2.py and the script will telnet to the switch and create various VLANs, but it's an inefficient way of adding VLANs or doing repetitive work.
Let's see if we can clear up this code and do it better.
Now to do that we're going to create a loop. A loop is essentially telling the computer to do something for a certain amount of time.
So in this example I'm going to do a range. I'm going to say do the following in the range 2 to 10. Now in Python, spacing is very important. Think about how the Cisco IOS works or the IOS of many networking devices work. When we’re looking at code so as an example, this code or configuration if you prefer of a Cisco router, do you agree that this space is very important?
This space indicates that this IP address is configured under this loopback. All of this code here belongs to this interface and so forth and so on. We know that this network command belongs under the OSPF routing process.
If I try to type network 0.0.0.0 here in global configuration mode, the router doesn't accept it that space is telling us that this piece of code or configuration belongs under the routing process. This code belongs under this interface. So use that as an analogy for what we're doing here.
The code that we're now going to write belongs under the for-loop. So what we're going to write to the switch is vlan + n + carriage return.
So those two lines will allow us to replace the code that we've got here. What I'll do is paste that back but notice I'm going to press tab. And what we want to do here is say + n + carriage return.
Now when we run the script it's going to fail because this is an integer and this is a string and I'll talk about that in a moment.
But essentially what we've done is we've replaced all of this code with three lines and we can extend that by for instance doing 2 to 20. So what I'll do is I'll save that code and I'll run it but notice the problem when we run the code we’re told that line 23 in our script, in other words this line is mixing strings and integers.
So I'll run Python directly and notice what we’re told when we enter type ... https://www.youtube.com/watch?v=dE2afwB9d5U
42701214 Bytes