In the previous video, we already install netmiko on ubuntu server. 2
Next in this video, we are going to learn how to do network automation using netmiko. 3
Let’s see on the documentation. 4
First, we need to import connect handler function from netmiko. 5
The script is From netmiko import connect handler. 6
And then here we need to create a dictionary to shave the router information. 7
You can use anything for the variable name, in this example, the variable name is cisco 881. 8
Here the device type is cisco ios, this is means that we are going to do network automation 9
on cisco devices. 10
If we want to do network automation on other vendors, we can click on this ssh_dispatcher 11
to see the other device type. 12
In the ssh dispatcher, we can see in the CLASS_MAPPER section, let’s just copy this, go to ssh 13
dispatcher and then ctrl f to find, paste it here. 14
Okay here we can see many device type that we can use. 15
For example, if we want to do network automation on aruba devices, so the device type is aruba_os. 16
This is that we are use, which is cisco_ios. 17
Or if you want to do network automation on cisco nexus series, so the device type will 18
be cisco_nxos. 19
If You want to remote cisco ASA Firewall, So the device type will be cisco_asa 20
Here we also have juniper, mikrotik, linux, and others, we have so many choices here. 21
Okay let’s back to the documentation, here we need to define host, this is the ip address 22
of the router, username, this is the username to remote the router using ssh, 23
and this is the password. 24
We also can define the port of ssh, but this is optional, we only need to define this parameter 25
if we change the default ssh port on the cisco router. 26
Here we also can define enable secret, for now, the user is in privilege 15, 27
so when we login using that user, we are in the privilege mode directly, so we don’t need to define 28
enable secret parameter here. 29
After defining the dictionary about the router, next, we can connect to the router, 30
this is very simple, we just need to call ConnectHandler function from netmiko. 31
And then the argument is the dictionary that we have created, we need to use stars (*) two 32
time like this if the argument is a dictionary. 33
After that, we can send the cisco command. 34
Let’s say we want to check the cisco interface, so we can send command show ip interface brief. 35
And then we can print the output. 36
Okay, this is more simple than paramiko right? 37
Let’s try to write the netmiko script on sublime. 38
Let’s create new folder, let’s say the name is lab-netmiko. 39
And then here inside lab_netmiko, let’s create new file, let’s say the name is netmiko_basic.py. 40
Now let’s try to write the netmiko script, first, let’s import connect handler function 41
from netmiko. 42
The script is From netmiko import connect handler. 43
Now let’s create a dictionary to save an information about the router. 44
We will try to automate router 1 first, so let’s say the variable name is router 1, 45
the device type is cisco ios, 46
the host is the ip address of router 1, which is 10.10.10.1, the username is cisco, 47
and the password is also cisco. 48
We don’t need to define port and secret for now, because the ssh port on router 1 is 49
default, which is 22, and our user is already in the privilege 15. 50
Now let’s connect to the router, net_connect, this is just a variable, so you can use anything here, 51
let’s say we use conn, and then connect handler, and the dictionary name, which is r1. 52
After connect to the router, we can send the cisco command, let’s try to see the interface, 53
output, conn, this is the variable that we already defined before. 54
And then send_command show ip interface brief, 55
last, let’s print the output. 56
Okay we have done here. 57
Save, let’s go to ubuntu server, ls, here we can see that now we have lab_netmiko, 58
let’s cd to lab_netmiko, ls, here we can see that we have netmiko_basic.py. let’s execute this. 59
Here we can see that we are success to print the interface of router1, 60
But there is no description about the router, let’s modify the script. 61
Before print the output, let’s print the router ip address, so here print interface 62
for router, we can use f string here, and then here we can define the dictionary name, 63
which is r1 and let’s access the host key. 64
Oh sorry, here we already use doble quote, so here we must use single quote. 65
Save, 66
Let’s back to the ubuntu server, execute the script again. 67
Okay here we have description, interface for router 10.10.10.1, and this is the interfaces. 68
Okay I think enough for this video 69
Thankyou for watching and see you on the next video.