Okay.
So let's edit our Python three three script.
What we want to do here is replace all our VLAN code with a loop.
So I'm going to paste a loop in.
This is very similar to what we saw previously for MN in range 2 to 11.
Rather than simply printing the information to the screen.
We're going to write it to the switch in a similar way to what we did previously.
So we're going to write a VLAN followed by a string.
We have to convert and which is an integer to a string here.
And then encoded as ASCII text when sending it to the switch.
The reason for doing that is the following.
So let's go back to our range script.
If I edited our script and said Print
VLAN plus PN, we'd get an error, so let's try that.
So Python three.
Python ranged at p y.
Notice the error.
Python doesn't like the statement because this is an integer and this is a string.
So let's talk a little bit about that.
If I say X equals one.
And then use type to check what X is.
We told that x is an integer or int integers or whole numbers.
If I say y equals to type the type of y, the type of y is integer.
Those are both integers.
So X plus y is accepted.
I could say that Z or z equals x plus y and z or z equals three.
But if I said A equals hello and print a day that works.
But it doesn't work like this.
We can't add strings and integers.
If you want to do that, we have to convert the integer to a string.
And notice it.
Concatenate them.
It doesn't add them together.
You can't add strings.
Like you can add mathematical values.
You concatenate them together.
So if I said a plus string Y plus string x, that's what we're going to see play 21.
That's different to X plus Y.
So again, I could concatenate one x like that, which is different to x plus y or why plus x?
This is concatenation.
This is addition.
So when we run our script.
We need to tell Python that we're going to convert this integer value to three, four, five, six,
seven, eight, nine and ten to a string.
And then we're going to concatenate it with the word VLAN.
We're going to encode this as an ASCII character and then press enter.
So essentially what that's doing is sending that to the switch or that or this in the range 2 to 10.
Then it's going to set the name of the VLAN.
So name python underscore, vlan underscore and are the number in the range.
Again, these are a string of values encoded as ASCII and sent to the switch.
So on the switch at the moment we have a vlans 1 to 8.
I'm going to remove those vlans.
So at the moment we have VLAN one only as our ethernet vlan.
But let's save our script and see what happens.
So.
Python three.
Python three, three.
P y.
We asked for our username.
Ask for our password.
And as you can see it's toll leading to the switch.
Show VLAN brief.
You can see that VLANs 2 to 10 were created on the switch.
So in other words, we've replaced.
These lines of code.
What's these three lines of code?
And if we wanted to create more VLANs, we could simply change this one entry.
And this is the power of automation.
I'm simply changing that one entry.
And if I run the script again, what we should see is that 20 VLANs are created on the switch show line
shows us that the script is telnet to the switch.
It's now configured the switch.
So show vlan brief.
Notice we have 20 ethernet vlans configured on the switch.
And again I could simply edit.
But that one entry make it 101 and run the script again.
And what we should see is that 100 VLANs are created.
Show line shows us that the script is telnet to the switch once again.
Show VLAN shows us that VLANs are being created.
At this point it's up to.
52 villains.
And let's use brief here.
Now it's got up to.
81 VLANs and as you can see, it's now completed running the script.
So show VLAN brief again.
We should see that 100 vlans are configured on the switch.
So once again, by only making a small change in our script, we were able to configure many VLANs on
the switch.
Now you may be thinking, well, I could simply paste the VLAN information from an Excel spreadsheet
or from notepad onto the switch.
But again, don't lose sight of the fact that this is the beginning.
We are going to iterate and improve our scripts as we go along.
We're going to run loops within loops.
We want to be able to configure many switches at the same time using one script.
And later on, you could even make your scripts more intelligent where you use if statements.
So if this is true, then do that.
But if it's not true, then do something else.
This is just the beginning, but hopefully it shows you that it's possible to automate Cisco Networks.
And the same applies to other network devices using Python.
We could, as an example, do another step here.
Where we say Save the configuration once you've run the script.
I'll run the script again.
You can see it's telnet into the switch show line.
This is the v t y line used by the script.
Script is completed and as you can see here, the configuration of the switch was saved.
So just think about how you can scale this to do configurations on many network devices rather than
just a single switch.
But hopefully already you can see the potential of using a network automation.