So I copied this script from the Python website.
Now for this initial script, I'm going to do the editing directly in the network automation container.
Ellis shows us that we have no files on the network automation container.
I'll create a file called Python 31py and I'll paste the script from the Python website into the network
automation container.
One of the advantages of using Python over other programming languages is all of the modules and additional
code that you can leverage.
So rather than writing everything yourself, you can simply import a libraries such as the Telnet library
into your Python script.
So in this example, we're going to import get pass.
We're going to leverage that here by asking the user to enter their password rather than storing the
password in the script.
We're going to ask the user to enter their password and then save that in what's called a verbal.
A variable is a placeholder that can change in value.
So as an example, I could enter a password of Cisco.
You might enter a different password in your topology that's going to be stored in this verbal that's
then going to be sent to the router.
At this point, now, the first thing the script needs to know is what a device are we going to telnet
to?
In this example, it's configured to Telnet to itself, and that's not what we want to do in this network.
So one of the first things we need to do is configure the router with an IP address and a username and
password so that the network automation container can telnet to the router and configure it.
So I'll give the router a name of router one and then on gigabit zero zero I'll configure an IP address
of 1921681 2271.
Now the reason for doing that is the network automation container has been allocated to this IP address
in this subnet by the net cloud.
This is a slash 24 subnet.
Network automation containers using this IP address once again.
So I'll configure the router with an IP address in the same subnet and I'll shut the interface.
So hopefully at this point the network automation container should be able to ping this IP address of
the broader because the interface is now come up.
And there you go.
The network automation container can ping the router, but at this point won't be able to telnet to
the router.
Notice the connection is refused.
Before you automate, make sure that things work manually.
So in other words, you should first test that things work and then you can automate.
So as an example, one of the things we want to configure here is a username and a password of, let's
say, Cisco.
In these initial examples, I'm not trying to do everything securely.
I simply want to get you started.
In programming, we have this concept of iteration.
Start something, then iterate or improve your scripts as you go along.
And I think that's a very important skill just to get started and then improve things as you go along.
So in this example, I want to simply configure things so that you can see an example of network automation.
In this example on the V lines, I'm going to use the command login local so that the script gets prompted
for a username and password.
And I'm going to say transport input all.
Now, you could tie that down to S-H and Telnet.
But because this is a lab, I'm not too worried about that.
So let's try and telnet again.
We can see that we prompted for our username and then our password.
So exit out of the rudder on back on the network automation container.
I'll save the router's configuration.
So let's ed the script by using Nano.
The IP address that we're going to connect to is 1921681 2271.
In other words, it's the IP address of the router.
We then going to prompt the user to enter their username?
So rather than prompting them for this, let's ask them to enter their telnet username.
So what should happen is the script should prompt the user for their telnet username and their password
and then it will telnet to the router based on this available.
So we configuring a variable here with the IP address of the router.
We are hard coding values in the script.
Now programmers with a lot of experience will tell you that you shouldn't.
Hard code values.
That's true.
But again, we'll get to that later.
For now, we simply want to get started automating the network so we're going to hard code or that value
and then later on we'll improve the scripts.
So the host of variable is configured with this value.
After this point in the script and then at this point the Telnet library is being used to Telnet to
the host that we specified over here.
We are leveraging the Telnet library that someone else wrote in our script.
So rather than us developing a low level Telnet program, we're going to simply leverage or reuse the
code that's then going to be stored in this variable.
And then we're doing something at this point.
The script's going to wait until it sees a log in.
And that's actually incorrect because when we telnet to the router and I'll just telnet locally, what
it should look for is the username, so I'll copy that.
And I'll edit this line because what we're looking for is username and not login.
When the script sees a username, it's going to write.
In other words, it's going to send.
The username that the user entered at this point to the router.
It's using ASCII encoding here and then it's going to follow that with a carriage return.
So in other words, it's going to type your username.
David Press Enter.
That's logically what's happening in the background.
Then the script says if a password was configured, in other words, we entered a password at this point.
It's going to wait until it sees password.
That's correct when telnet to a Cisco router.
So when it sees that it's going to write the password that was entered here.
So in other words, it's going to send the password to the router in ASCII format and press enter again,
enter the username and then the password.
And hopefully at that point we'll be logged into the router.
Now here it's trying to write a Linux command to the router.
That command is not supported on a Cisco router.
What we want to do is type enable so that we can go to enable mode.
At this point, no password has been set.
So going back to the console of the router, we need to set or create an enable password.
And again, I'll simply set it to Cisco.
This is why it's important to test things, make sure that they work manually before you automate.
So again.
And to use a name and to password at this point then type enable.
So enable press enter.
Now we need to enter the enable password.
So what I'm going to do is copy that line and paste it here.
And I'll send a password of Cisco to the broader.
Now again, you don't want to embed passwords in your scripts like this.
Later on, we'll improve the script.
But to get started will embed the password in the script.
So we're going to enter a password of Cisco and that will take us to privilege mode or enable mode on
the broader.
Now, once we there, we want to do something.
So as an example, we want to go to global configuration mode and then we want to create a loopback
interface.
So let's say loopback interface zero.
And then we want to configure an IP address on the loopback interface, something like that.
And then we want to exit.
Or in our example, before we exit the Telnet session, we want to type end.
So that we go back to privilege mode.
And then we exit out of the Telnet session.
So again when we telnet to the router username password enable password quantity interface loopback
zero IP address creator the IP address.
On the rudder type end and exit out of the rotor.
That's the logic of what our script does.
So I'm back on the console of the rudder.
What I'll do is I'll remove the loopback interface.
So show IP interface brief.
Do that again.
No loopback is currently configured on the router.