Now.
Previously we created a script.
Python 32. p y.
If I look at the contents of that file, we can see that the network automation container telnet to
a switch that's hardcoded in the script.
And then creates some VLANs on that switch.
So using this as our basis, let's now extend the script to use a file of IP addresses.
So rather than hard coding an IP address in the script, let's open a file called My Switches.
What you'll notice is the script is very similar to what we created previously.
This code is essentially the same.
Except here we've got a hardcoded IP address here have simply left host equal to local host and notice
at a line eight we change things because we're going to open up a file called my switches and only later
are we going to telnet to the device.
Unlike what we did in our original script.
The reason for doing this is we want to open up a file called My Switches, and then for each line or
entry in that file we going to telnet to the switch and create the relevant VLANs.
So rather than only telnet into an individual switch and making changes on that individual switch,
we're going to telnet to multiple switches and make changes on them.
So the first thing we need to do is create a file called My Switches.
You could call this anything, but that's the name of the file in my script.
What this is going to contain is a list of IP addresses.
So I'll copy and paste that a few times.
This is simply the IP addresses of devices that we want to make changes to or changes on, make sure
that you don't have any spaces.
Later on, I'll show you how to strip lines in spaces.
But for now, try and remove all of the additional spaces and carriage returns in your file.
I'll save that file.
So, Kat, my switches.
That's what my switches looks like.
Let's create another test.
Python script.
So Python file p y.
Let's copy this entry.
This opens a file.
So we're going to open up my switches, and then we're going to do something.
I'm going to say four IP address in F so for each a line in the script.
Four spaces.
Print and lets print the IP address.
So it's a fairly simple script.
All it's going to do is open up a file and print the lines in the file and there you go.
Notice it's printed the entries in the my switches file.
So again, the python filed on p script.
Simply opens a file that you specify.
In this case, it's my switches.
And then for each align in the file, it prints the line.
Now you could call this anything.
You could call this a line as an example.
So for each line in the file f print the line.
And there you go.
The result is the same, but because we have IP addresses in our file.
I'm going to use IP.
And again, notice the result is the same.
So what we've got here is a script that opens a file.
It strips any trailing spaces, so that removes an error.
If you have an IP address of 121681 2282 space that removes or strips any spaces after the IP address.
To ensure that we only have the IP address and not IP address space.
The script then prints configuring and the IP address from the file.
Now this is a little bit redundant, but I'm going to keep it in the script to keep it consistent with
our previous scripts.
In our previous scripts.
We had host set to a hardcoded IP address.
Here I have left it to the default of a local host.
However, after this point, the host of variable is going to be replaced with the IP address of the
switch in the file.
This is now consistent to what we had previously in our script.
But notice the host verbal is set to the IP address of the switch in the file.
So what you'll notice is this is a very similar to what we have in our original script.
And then here we are going to global configuration mode and creating our VLANs.
We don't have to go to enable mode because when we telnet to the switches, we've configured them.
To take us immediately to privilege mode.
Notice I'm in privilege mode already.
That's because the username David has.
Level 15 privileges, as you can see there.
So all we going to do is create some VLANs.
And then we're going to print the results.
Now, we'll optimize this in a moment.
We want to improve the script by running loops within loops, but initially will start with a single
loop.
Configuring Individual VLANs.
So let's create a script called Python 35. p y.
I'll paste that script into the network automation container and save the script.
So again, more Python 35. p y.
That's the script that we've just copied.
Before I run the script.
Notice that switch to only has the default VLANs.
I'll do a debug telnet on the switch.
So we can see connections to the switch on switch for show vlan brief.
We only have the default vlans.
Now you could do something similar on all the other switches, but I'm not going to do that.
They all are the same.
Show vlan brief on switch six.
Notice only has the default vlans.
Switch one has more vlans.
Because we configured those vlans previously.
So show vlan brief shows us that we have a lot of vlans on the switch.
So I'm going to say no vlan one two, 100.
And I should say too because vlan one cannot be deleted.
So show vlan brief.
We now only have the default VLANs.