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