Now that we've been able to telnet from the network automation container to the router and make changes
to it, let's do something similar with the switch.
So on the network automation container, we currently have a script Python three one API.
I'm going to copy that script to another script called Python 32. p y.
So we've now got two scripts and what I'll do now is edit python 32. p y.
In this example, we want to configure a different device.
So let's configure the switch with IP address.
1921681 2272.
He has the console of the switch.
So I'll give it a name.
Switch one.
Do show IP interface brief.
Shows us interfaces on the switch.
What I'll do here is configure.
The IP address on VLAN one
slash 24 mosque and I'll no shut interface VLAN one so show IP interface brief again.
We've now got vlan one configured with this IP address and the interface is up, up.
What I now need to do is configure a username to be consistent.
I'll keep it the same configure and enable password.
In the real world you may want to use a secret password, but this is fine for our examples and allows
us to verify our scripts as we can see the usernames and passwords used log in.
A local is going to be used on the v lines and we're going to allow all protocols to access the v t
lines.
So back on the network automation container.
I'll save that script.
Let's confirm that we can ping the switch.
Yes, we can.
Can we telnet to the switch?
Username is david.
Password is cisco type enable.
And the answer is yes, we can telnet and access enable mode onto the switch from the network automation
container.
So on a script are we going to telnet to the switch IP address one on 21681 2272 we're going to prompt
the user for the username and password and at this point the script is going to telnet to the switch.
It's going to look for username.
So again, we can confirm that the same process is used on the switch by telnet to the switch.
So telnet to the switch.
Your username is what's prompted for and then password.
So this code can be reused for the telnet connections to the switch.
All I've done at this point is change the IP address.
On the switch.
We need to tap, enable and the password of Cisco so that stays the same.
We then go to global config mode and rather than creating loopback, let's configure VLANs on the switch.
So I'll configure VLAN two and give it a name.
Of Python.
VLAN two.
So basically on the switch, we're going to go to global config mode type VLAN two and then configure
a name of Python VLAN two.
So let's do it like this.
So hopefully when we run the script, we should see a result like this.
So what I'll do is remove vlan two from the switch show vlan
brief.
We only have the default vlans on the switch.
What I'll do here is delete a lines by using control k and then I'll copy these lines into the script.
Now programmers with lots of experience will tell you that you should use loops and not duplicate code
like we're doing here.
Again, that is true.
We'll get to that later.
For now, let's configure VLANs two, three, four and five on the switch.
Later on, we'll use a loop to configure 100 VLANs on the switch.
After the moment we only have VLAN one as the ethernet VLAN.
We're not going to worry about these vlans.
They are not supported on the switch anyway.
I'll save the script.
Python three.
Python 32. p y.
Username is David.
Password is cisco.
Before I press enter debug telnet on the switch.
Press enter now.
We can see that the script is telling to the switch.
We can see on the network automation container that vlans were created on the switch.
So show VLAN brief.
We can see that those VLANs were created on the switch.
So it was as simple as that.
To configure VLANs on a Cisco switch using Python, I could copy those lines of code and edit those
lines to create more VLANs.
So let's create.
Eight VLANs on the switch.
For example, so VLAN six, seven and eight will now be created on the switch back on the switch once
again we only have VLANs.
One, two, three, four and five.
But if I run the script again.
Script telnet to the switch and has created these additional vlans.
So show vlan brief.
They are the vlans on the switch.
So we've now got two scripts.
Python three.
Python three, one.
P y makes changes to the router.
Three two makes changes to the switch.
These are, again, very simple scripts.
But hopefully it shows you how you can configure network devices using Python with just a few lines
of code.
This is not a very large script, but we've been able to use the script to make changes to the network
devices.
Now again, you may want to use a more powerful.
I.D. to make your changes so you could use something like sublime text, pie, charm or something else
to do your actual editing, and then you can simply paste those scripts into the network automation
container.
I'm going to do that later in the course as our scripts become more powerful and more complicated.
I just want to get you started automating once you get started and see what's possible.
Hopefully you can think of other scenarios and other things that you can do using Python to automate
your network.