GNS3 Talks: Python for Network Engineers with GNS3 (Part 4) - Create switch VLANs using loops
48.4 هزار بار بازدید -
7 سال پیش
-
Udemy: Get the course for
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: Python Programming for Network Engineers
Script used in this video is available on GitHub here:
https://github.com/davidbombal/python...
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 1 in brackets? We’re told that this is an integer. Type david that's a string or type it this way is a string. But type let's say 1.5 is a floating value.
Integers are whole numbers floating numbers as an example, allow us to have fractions in our numbers. Now we’re told once again that we can't mix string and integer objects. So we're not going to let that stop us. What we're going to do here is change the integer to a string. And all we're going to do is type string or str (n) to change it to a string value and save the script.
Now on the switch at the moment, show vlan shows us that we have Ethernet VLANs 1 to 8
configured. We're not going to worry about token ring and FDDI. But what happens when we run our script?
So David, put the password in and notice what happens. It's creating VLANs 2 to 9.
So previously we had 1 to 8 now show vlan brief shows us that we have VLAN 9 and this is the power of loops.
If we want to add let's say the VLANs 2 to 20, we simply change that one value, run our script again. Notice it's created VLANs 2 to 19.
So show vlan brief, notice the VLANs are created.
And one thing I should say is, if we want to create 2 to 20, we need to add one extra value there.
So let's run the script again. this is the great thing about programming. If you make a mistake,
you can just correct it. And what's great with GNS3 is you can actually see what your program or script is doing.
So notice we have VLAN 20 created. So it was as simple as that to write a script that creates multiple VLANs on a Cisco switch. It's not a very long script it, only has a few lines in it.
Let's be a little bit ridiculous now and this is the power of programming. I'll create a hundred VLANs on our switch.
So run the script again and while that's running in the background, notice the VLANs are being created. We can see 27 VLANs, now 32 VLANs, now we have 36.
Now you may say that you could copy and paste from an Excel spreadsheet but remember with loops we can now create loops within loops. So what we could do is rather than just connecting to one switch, we can loop across multiple switches and run this code across multiple switches.
GNS3 Academy: Get the course for $10 here: https://goo.gl/vnZJhg
More free Python videos here: Python Programming for Network Engineers
Script used in this video is available on GitHub here:
https://github.com/davidbombal/python...
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 1 in brackets? We’re told that this is an integer. Type david that's a string or type it this way is a string. But type let's say 1.5 is a floating value.
Integers are whole numbers floating numbers as an example, allow us to have fractions in our numbers. Now we’re told once again that we can't mix string and integer objects. So we're not going to let that stop us. What we're going to do here is change the integer to a string. And all we're going to do is type string or str (n) to change it to a string value and save the script.
Now on the switch at the moment, show vlan shows us that we have Ethernet VLANs 1 to 8
configured. We're not going to worry about token ring and FDDI. But what happens when we run our script?
So David, put the password in and notice what happens. It's creating VLANs 2 to 9.
So previously we had 1 to 8 now show vlan brief shows us that we have VLAN 9 and this is the power of loops.
If we want to add let's say the VLANs 2 to 20, we simply change that one value, run our script again. Notice it's created VLANs 2 to 19.
So show vlan brief, notice the VLANs are created.
And one thing I should say is, if we want to create 2 to 20, we need to add one extra value there.
So let's run the script again. this is the great thing about programming. If you make a mistake,
you can just correct it. And what's great with GNS3 is you can actually see what your program or script is doing.
So notice we have VLAN 20 created. So it was as simple as that to write a script that creates multiple VLANs on a Cisco switch. It's not a very long script it, only has a few lines in it.
Let's be a little bit ridiculous now and this is the power of programming. I'll create a hundred VLANs on our switch.
So run the script again and while that's running in the background, notice the VLANs are being created. We can see 27 VLANs, now 32 VLANs, now we have 36.
Now you may say that you could copy and paste from an Excel spreadsheet but remember with loops we can now create loops within loops. So what we could do is rather than just connecting to one switch, we can loop across multiple switches and run this code across multiple switches.
7 سال پیش
در تاریخ 1396/01/18 منتشر شده
است.
48,452
بـار بازدید شده