1 00:00:00,291 --> 00:00:08,698 In this section, we are going to start learn about network automation. We will use paramiko first. 2 00:00:09,216 --> 00:00:14,576 In the previous video, we already learn how to install python package. 3 00:00:15,000 --> 00:00:22,423 And now we already have paramiko, let’s check it, pip3 freeze, 4 00:00:27,019 --> 00:00:30,693 Here we can see that we already have paramiko. 5 00:00:31,259 --> 00:00:37,986 To make sure that paramiko is installed correctly, we can go to python interpreter, 6 00:00:39,123 --> 00:00:42,041 and then try to import paramiko. 7 00:00:43,830 --> 00:00:52,114 If we don’t get an error like this, so paramiko is installed correctly. Exit here. 8 00:00:53,339 --> 00:00:58,970 Go to sublime, let’s create a new folder here, 9 00:01:01,843 --> 00:01:04,903 let’s say lab-paramiko. 10 00:01:06,950 --> 00:01:16,461 Inside lab-paramiko, let’s create new file, let’s say paramiko_basic.py. 11 00:01:20,211 --> 00:01:35,253 Save this, let’s check in the ubuntu server, here we can see that we have folder lab_paramiko, 12 00:01:35,664 --> 00:01:43,863 let’s see inside lab_paramiko, here we can see that we also have paramiko_basic.py. 13 00:01:44,766 --> 00:01:50,172 Now let’s write a python script to do network automation. 14 00:01:50,639 --> 00:01:54,343 I already write the script to do network automation, 15 00:01:54,784 --> 00:02:02,061 so I will just copy and paste the script, and then I will explain the script to you. 16 00:02:03,283 --> 00:02:11,720 Okay this is the script, we have 24 lines of code here. Let’s learn this code. 17 00:02:12,200 --> 00:02:19,685 First, here we import the paramiko. Here we also import package time and getpass. 18 00:02:20,347 --> 00:02:25,197 So this is some package that we are going to use in this code. 19 00:02:26,403 --> 00:02:35,494 Paramiko is not installed by default, so we need to install it using pip3 install paramiko 20 00:02:35,494 --> 00:02:38,165 like we do in the previous video. 21 00:02:38,723 --> 00:02:46,138 But time and getpass is installed by default, so we don’t need to install it anymore. 22 00:02:47,043 --> 00:02:52,514 we will talk more about the function of time and getpass later. 23 00:02:53,024 --> 00:03:00,933 For paramiko, the point is, we will use paramiko to remote the router using ssh. 24 00:03:01,579 --> 00:03:07,426 For example, in the normal way, if we want to remote the router, 25 00:03:07,426 --> 00:03:16,711 we can open a terminal and then type ssh username@ the ip address of the router. 26 00:03:17,024 --> 00:03:23,671 and then we need to type the password and we success ssh to the router. 27 00:03:25,579 --> 00:03:28,693 Okay, so that is the normal way. 28 00:03:29,030 --> 00:03:36,317 Now, if we want to do network automation, we need to remote the router from python. 29 00:03:36,934 --> 00:03:43,111 Paramiko is a package to remote the router using ssh from python. 30 00:03:44,384 --> 00:03:48,151 Next here we ask input from user, 31 00:03:48,520 --> 00:03:55,605 and then the data that inputed by user will be saved in the ip_address variable. 32 00:03:56,275 --> 00:04:02,810 Here we also ask an input from user and save the data to username variable. 33 00:04:03,630 --> 00:04:07,450 For the password, we use getpass library, 34 00:04:08,003 --> 00:04:18,453 so getpass.getpass. This is means that we call getpass function inside getpass library. 35 00:04:19,259 --> 00:04:23,818 So the first getpass is the name of the library or package, 36 00:04:24,035 --> 00:04:30,336 and the second getpass is the name of the function inside getpass library. 37 00:04:30,651 --> 00:04:33,160 What is the function of this script? 38 00:04:33,598 --> 00:04:38,607 This is same as input, but the differences is, 39 00:04:38,694 --> 00:04:44,730 if we use getpass, everything that we type will not shown in the terminal. 40 00:04:45,240 --> 00:04:54,336 Let’s try it, go to python interpreter, let’s try input first, 41 00:04:54,849 --> 00:05:03,733 Ip_address = input and then ip address. Enter. 42 00:05:03,918 --> 00:05:14,453 Here we need to input the ip address, let’s say 192.168.1.1, let’s print the ip_address, 43 00:05:18,544 --> 00:05:25,709 okay here we can see that the value of ip_address is 192.168.1.1 44 00:05:26,483 --> 00:05:32,053 Now let’s try to use getpass, let’s import it first, 45 00:05:36,624 --> 00:05:43,647 And then password getpass.getpass, enter this, 46 00:05:43,899 --> 00:05:49,204 Here we can type the password, let’s say 123. 47 00:05:49,867 --> 00:05:54,412 Here we can see that we can not see anything that we type. 48 00:05:54,412 --> 00:05:59,634 Let’s just enter here, and then let’s print the password. 49 00:06:00,323 --> 00:06:08,544 Here we can see that the value of password is 123. So remember that the function of getpass 50 00:06:08,544 --> 00:06:15,789 is just like input, but everything that we type will not shown in the terminal. 51 00:06:16,963 --> 00:06:22,071 Okay, then here we create a variable ssh_client, 52 00:06:22,680 --> 00:06:28,085 this variable is a ssh client function from paramiko. 53 00:06:30,131 --> 00:06:38,896 Next here we define set missing host key policy auto add policy. This is means 54 00:06:38,896 --> 00:06:44,306 that we add the key from the router automatically to the ubuntu server. 55 00:06:44,667 --> 00:06:53,826 If you remember, when we remote a router for the first time, we need to type yes. Let’s scroll, 56 00:06:55,627 --> 00:07:03,802 Here we can see that we remote router 3, and here we need to type yes to continue. 57 00:07:04,294 --> 00:07:07,733 So that is the function of this script. 58 00:07:09,288 --> 00:07:18,823 Next, here we connect to the router. Here we have hostname, this is argument from paramiko. 59 00:07:19,334 --> 00:07:28,047 So we need to use hostname, we can not change this, the hostname is ip_address variable. 60 00:07:28,238 --> 00:07:35,714 This is a variable that we already created before. The username is username variable, 61 00:07:36,779 --> 00:07:40,466 and the password is password variable. 62 00:07:41,203 --> 00:07:50,410 If the variable that you create is not username, let’s say user, so here will not username, 63 00:07:50,534 --> 00:07:58,213 but user. So remember that this is based on variable that we have created. 64 00:07:58,798 --> 00:08:09,358 Let’s say again the variable is not password, but passw, so here we also need to use passw. 65 00:08:10,840 --> 00:08:15,000 Here we print that we success login to the ip address, 66 00:08:15,278 --> 00:08:20,442 so remember that this the variable that we have created before. 67 00:08:21,246 --> 00:08:27,906 Next here we invoke shell, this is means that we go to the cisco shell. 68 00:08:28,440 --> 00:08:31,893 After that, we can type the cisco command, 69 00:08:32,478 --> 00:08:35,634 Herer we can see configure terminal, 70 00:08:35,634 --> 00:08:46,298 and then backslash n, this is means enter, so here we type configure terminal and then enter. 71 00:08:46,846 --> 00:08:55,734 Next here we create interface loopbcak0, and don’t forget to enter, the ip address 72 00:08:55,734 --> 00:09:07,654 is 1.1.1.1 255.255.255.255, once again, don’t forget to use backslash n or enter. 73 00:09:08,379 --> 00:09:15,155 So the point is, this script is used to create interface loopback 0 in the router, 74 00:09:15,155 --> 00:09:20,466 and the ip address for loopback0 is 1.1.1.1 75 00:09:21,118 --> 00:09:31,629 But this is public ip address, let’s change this become private ip address, let’s say 10.1.1.1. 76 00:09:32,878 --> 00:09:36,317 Next here we have time sleep 1, 77 00:09:36,619 --> 00:09:44,533 this is means that the script will stop for 1 second to wait the cisco command is executed. 78 00:09:45,118 --> 00:09:49,174 May be some of you have a question, how if we not 79 00:09:49,174 --> 00:09:55,813 use time sleep? We will answer that question later in the next video. 80 00:09:56,563 --> 00:10:02,114 Okay, next here we save the output to the output variable. 81 00:10:02,606 --> 00:10:11,389 Here 65535 means that we save 65535 character. 82 00:10:12,089 --> 00:10:20,213 And then decode here. In python3, the type of the outuput is byte, 83 00:10:20,454 --> 00:10:24,442 so we need to decode the result like this. 84 00:10:25,000 --> 00:10:32,082 Later in the next video, we will try to remove this decode and see what happened. 85 00:10:33,184 --> 00:10:40,661 Here we print the output. And last, here we close the ssh connection. 86 00:10:41,254 --> 00:10:42,608 Save this. 87 00:10:43,166 --> 00:10:49,280 If we see circle black like this, this is means that the script is not saved yet, 88 00:10:50,240 --> 00:10:55,487 if we already save the script, the circle black will gone like this. 89 00:10:56,360 --> 00:11:11,242 Now let’s go to ubuntu server, exit, ls, let’s go to lab paramiko directory, cd lab_paramiko. Ls, 90 00:11:11,782 --> 00:11:20,114 Here we can see paramiko_basic.py, this Is the script that we just create in the sublime. 91 00:11:20,680 --> 00:11:29,018 Let’s run this script, but before that, let’s make sure that the router is don’t have loopback0 yet, 92 00:11:29,352 --> 00:11:37,428 let’s check on router1, show ip interface brief. 93 00:11:41,603 --> 00:11:44,890 Here we can see that we have fast ethernet, 94 00:11:46,195 --> 00:11:51,757 a lot of serial interface, but we don’t have loopback0 yet. 95 00:11:52,360 --> 00:11:54,903 Now let’s run the script, 96 00:11:55,198 --> 00:11:59,476 Python3 paramiko_basic.py, 97 00:12:00,144 --> 00:12:08,379 Let’s remote the router 1, so the ip address is 10.10.10.1, the username is cisco, 98 00:12:08,379 --> 00:12:11,383 and the password is also cisco. 99 00:12:11,979 --> 00:12:20,415 Here we can see that we are success login to 10.10.10.1, and here we can see the configuration, 100 00:12:20,755 --> 00:12:32,888 configure terminal, interface loopback0, and the ip address is 10.1.1.1 255.255.255.255. 101 00:12:33,480 --> 00:12:38,855 this is means that we are success send this configuration to router 1. 102 00:12:39,947 --> 00:12:46,749 Let’s check on router 1 again, do show ip interface brief. 103 00:12:49,123 --> 00:12:57,807 Here we can see that now we have loopback0, the ip address is 10.1.1.1 104 00:12:58,355 --> 00:13:04,602 Okay so now we are success to do network automation using python. 105 00:13:05,000 --> 00:13:12,564 I think enough for this video, in the next video, we are going to learn more about time sleep. 106 00:13:12,974 --> 00:13:16,808 Thank you for watching and see you on the next video.