In this section, we are going to start learn about network automation. We will use paramiko first. 2
In the previous video, we already learn how to install python package. 3
And now we already have paramiko, let’s check it, pip3 freeze, 4
Here we can see that we already have paramiko. 5
To make sure that paramiko is installed correctly, we can go to python interpreter, 6
and then try to import paramiko. 7
If we don’t get an error like this, so paramiko is installed correctly. Exit here. 8
Go to sublime, let’s create a new folder here, 9
let’s say lab-paramiko. 10
Inside lab-paramiko, let’s create new file, let’s say paramiko_basic.py. 11
Save this, let’s check in the ubuntu server, here we can see that we have folder lab_paramiko, 12
let’s see inside lab_paramiko, here we can see that we also have paramiko_basic.py. 13
Now let’s write a python script to do network automation. 14
I already write the script to do network automation, 15
so I will just copy and paste the script, and then I will explain the script to you. 16
Okay this is the script, we have 24 lines of code here. Let’s learn this code. 17
First, here we import the paramiko. Here we also import package time and getpass. 18
So this is some package that we are going to use in this code. 19
Paramiko is not installed by default, so we need to install it using pip3 install paramiko 20
like we do in the previous video. 21
But time and getpass is installed by default, so we don’t need to install it anymore. 22
we will talk more about the function of time and getpass later. 23
For paramiko, the point is, we will use paramiko to remote the router using ssh. 24
For example, in the normal way, if we want to remote the router, 25
we can open a terminal and then type ssh username@ the ip address of the router. 26
and then we need to type the password and we success ssh to the router. 27
Okay, so that is the normal way. 28
Now, if we want to do network automation, we need to remote the router from python. 29
Paramiko is a package to remote the router using ssh from python. 30
Next here we ask input from user, 31
and then the data that inputed by user will be saved in the ip_address variable. 32
Here we also ask an input from user and save the data to username variable. 33
For the password, we use getpass library, 34
so getpass.getpass. This is means that we call getpass function inside getpass library. 35
So the first getpass is the name of the library or package, 36
and the second getpass is the name of the function inside getpass library. 37
What is the function of this script? 38
This is same as input, but the differences is, 39
if we use getpass, everything that we type will not shown in the terminal. 40
Let’s try it, go to python interpreter, let’s try input first, 41
Ip_address = input and then ip address. Enter. 42
Here we need to input the ip address, let’s say 192.168.1.1, let’s print the ip_address, 43
okay here we can see that the value of ip_address is 192.168.1.1 44
Now let’s try to use getpass, let’s import it first, 45
And then password getpass.getpass, enter this, 46
Here we can type the password, let’s say 123. 47
Here we can see that we can not see anything that we type. 48
Let’s just enter here, and then let’s print the password. 49
Here we can see that the value of password is 123. So remember that the function of getpass 50
is just like input, but everything that we type will not shown in the terminal. 51
Okay, then here we create a variable ssh_client, 52
this variable is a ssh client function from paramiko. 53
Next here we define set missing host key policy auto add policy. This is means 54
that we add the key from the router automatically to the ubuntu server. 55
If you remember, when we remote a router for the first time, we need to type yes. Let’s scroll, 56
Here we can see that we remote router 3, and here we need to type yes to continue. 57
So that is the function of this script. 58
Next, here we connect to the router. Here we have hostname, this is argument from paramiko. 59
So we need to use hostname, we can not change this, the hostname is ip_address variable. 60
This is a variable that we already created before. The username is username variable, 61
and the password is password variable. 62
If the variable that you create is not username, let’s say user, so here will not username, 63
but user. So remember that this is based on variable that we have created. 64
Let’s say again the variable is not password, but passw, so here we also need to use passw. 65
Here we print that we success login to the ip address, 66
so remember that this the variable that we have created before. 67
Next here we invoke shell, this is means that we go to the cisco shell. 68
After that, we can type the cisco command, 69
Herer we can see configure terminal, 70
and then backslash n, this is means enter, so here we type configure terminal and then enter. 71
Next here we create interface loopbcak0, and don’t forget to enter, the ip address 72
is 1.1.1.1 255.255.255.255, once again, don’t forget to use backslash n or enter. 73
So the point is, this script is used to create interface loopback 0 in the router, 74
and the ip address for loopback0 is 1.1.1.1 75
But this is public ip address, let’s change this become private ip address, let’s say 10.1.1.1. 76
Next here we have time sleep 1, 77
this is means that the script will stop for 1 second to wait the cisco command is executed. 78
May be some of you have a question, how if we not 79
use time sleep? We will answer that question later in the next video. 80
Okay, next here we save the output to the output variable. 81
Here 65535 means that we save 65535 character. 82
And then decode here. In python3, the type of the outuput is byte, 83
so we need to decode the result like this. 84
Later in the next video, we will try to remove this decode and see what happened. 85
Here we print the output. And last, here we close the ssh connection. 86
Save this. 87
If we see circle black like this, this is means that the script is not saved yet, 88
if we already save the script, the circle black will gone like this. 89
Now let’s go to ubuntu server, exit, ls, let’s go to lab paramiko directory, cd lab_paramiko. Ls, 90
Here we can see paramiko_basic.py, this Is the script that we just create in the sublime. 91
Let’s run this script, but before that, let’s make sure that the router is don’t have loopback0 yet, 92
let’s check on router1, show ip interface brief. 93
Here we can see that we have fast ethernet, 94
a lot of serial interface, but we don’t have loopback0 yet. 95
Now let’s run the script, 96
Python3 paramiko_basic.py, 97
Let’s remote the router 1, so the ip address is 10.10.10.1, the username is cisco, 98
and the password is also cisco. 99
Here we can see that we are success login to 10.10.10.1, and here we can see the configuration, 100
configure terminal, interface loopback0, and the ip address is 10.1.1.1 255.255.255.255. 101
this is means that we are success send this configuration to router 1. 102
Let’s check on router 1 again, do show ip interface brief. 103
Here we can see that now we have loopback0, the ip address is 10.1.1.1 104
Okay so now we are success to do network automation using python. 105
I think enough for this video, in the next video, we are going to learn more about time sleep. 106
Thank you for watching and see you on the next video.