1 00:00:00,820 --> 00:00:07,820 In the previous video, we already create a device list menu. Now in this video, we are 2 00:00:07,820 --> 00:00:14,360 going to create a Configure menu. This menu will be used to configuring the router 3 00:00:14,840 --> 00:00:23,380 Okay let’s just start. Open the visual studio code. First let’s create a new url, 4 00:00:27,120 --> 00:00:30,500 here I create configure url. 5 00:00:34,000 --> 00:00:39,660 This url will be redirected to configure function inside views.py. 6 00:00:39,880 --> 00:00:47,120 Now let’s create configure function in views.py. I will copy and paste the script from my note 7 00:00:47,120 --> 00:00:50,420 and I will explain to you what the script means. 8 00:00:51,460 --> 00:00:57,920 Okay we have a long script here. Before I explain about this script, let’s create 9 00:00:57,920 --> 00:01:04,780 the template file first. In this case, the name of the template file is config.html. 10 00:01:05,100 --> 00:01:12,720 I already upload config.html file in the resource section. So you can just download it and put it 11 00:01:12,720 --> 00:01:15,080 in the templates directory. 12 00:01:16,520 --> 00:01:23,960 Now let’s open the browser and navigate to 127.0.0.1:8000/configure. 13 00:01:24,840 --> 00:01:31,440 Here we can see the configure menu. We can chose which device that we want to configure. 14 00:01:32,680 --> 00:01:41,540 Here we can put mikrotik command, and here we can put cisco command. When we click this submit button, 15 00:01:41,540 --> 00:01:46,320 the application will send the command that we put here to the devices. 16 00:01:47,220 --> 00:01:52,220 Okay now let’s understand config.html file first. 17 00:01:55,840 --> 00:02:03,120 Here we define a looping to devices, this is a variable from the context. If we see 18 00:02:03,120 --> 00:02:07,360 in views.py, we have devices in the context. 19 00:02:08,360 --> 00:02:11,060 Let’s back to config.html. 20 00:02:13,100 --> 00:02:18,580 Here we can see a input check box, the value will be the id 21 00:02:18,580 --> 00:02:25,900 of each device. This choice will display the ip address and vendor of each devices. 22 00:02:27,320 --> 00:02:32,040 Next we have text area for Mikrotik & cisco command 23 00:02:32,580 --> 00:02:35,760 Last, we have a submit button here. 24 00:02:36,920 --> 00:02:44,500 Now let’s understand configure function in the views.py file. Here we can see an if 25 00:02:44,500 --> 00:02:52,100 statement. If the method of the request is post, this block script will be executed. 26 00:02:52,500 --> 00:02:58,580 The method of the request will be post if we click the submit button like this. 27 00:03:01,180 --> 00:03:08,320 But if we access the page directly from the url, the method of the request is get. 28 00:03:09,280 --> 00:03:15,940 If the method of the request is get, this block script will be executed. First we get 29 00:03:15,940 --> 00:03:24,860 all devices here, and then put all devices to the context. Last, we return a template file here. 30 00:03:25,280 --> 00:03:33,020 But if the method of the request is post, First we get list device, This device is the 31 00:03:33,020 --> 00:03:40,240 name of device choice in the template file. Here we can see that the name is device. 32 00:03:40,500 --> 00:03:48,000 Later, the device that we select in the browser, will be saved to selected_device_id variable. 33 00:03:48,980 --> 00:03:54,980 Next, we define mikrotik_command here. This mikrotik_command is the name of text area 34 00:03:54,980 --> 00:03:56,640 in the template file. 35 00:04:01,160 --> 00:04:04,320 And also we have a cisco command here. 36 00:04:05,780 --> 00:04:13,100 Next we looping to selected_device_id and then we get the device from the database based 37 00:04:13,100 --> 00:04:14,840 on the device id. 38 00:04:15,000 --> 00:04:23,220 Here we can see that we use get_object_or_404 function, so we need to import it first. 39 00:04:32,400 --> 00:04:39,800 Then here we call SSHClient function from paramiko. Because we use paramiko here, so 40 00:04:39,800 --> 00:04:42,340 we also need to import paramiko 41 00:04:51,000 --> 00:04:58,620 Here we connect to the router. We have a condition here, if the vendor of the device is cisco, 42 00:04:58,620 --> 00:05:06,380 We do invoke shell and then send configure terminal command. Here we looping to cisco_command, 43 00:05:06,380 --> 00:05:14,640 which is the variable that we have defined before. Here we send the command to the cisco device. 44 00:05:14,920 --> 00:05:20,040 Don’t forget to use time sleep if we doing network automation on cisco device 45 00:05:20,040 --> 00:05:21,560 using paramiko. 46 00:05:23,020 --> 00:05:30,500 Else, means that the device is not cisco, which is mikrotik, we looping to mikrotik_command 47 00:05:30,500 --> 00:05:38,220 and send the command to the device using exec_command. We don’t need to invoke shell & time sleep here. 48 00:05:39,140 --> 00:05:47,040 Last, when the configuration is done, we will be redirected to home page. Here we use redirect 49 00:05:47,040 --> 00:05:50,920 function, so we need to import it first. 50 00:05:56,860 --> 00:05:59,960 Let’s go to the browser, and refresh. 51 00:06:03,280 --> 00:06:06,620 Okay here we don’t have any error. 52 00:06:06,860 --> 00:06:14,400 We will not test the configuration function now, we will do the test later in the next video 53 00:06:14,900 --> 00:06:22,380 and this is the end of this video. In the next video, we are going to create a verify configuration menu. 54 00:06:22,540 --> 00:06:25,840 Thankyou for watching and see you on the next video!