2 - User Netmiko Python To Configure A Router
2 - User Netmiko Python To Configure A Router
PNETLAB Store
PNETLab.com
https://user.pnetlab.com/store/labs/detail?id=16043760603977
Lab Objective:
The objective of this lab exercise is for you to learn and understand Netmiko Python.
Task:
We have a router that had ssh configuration. And we will use Netmiko Python to collect some
information and configure this router.
Solution
Basic Configuration:
Router R1
Configuration hostname R1
!
ip domain name example.netacad.com
crypto key generate rsa modulus 2048
!
username cisco privilege 15 password 0 cisco123!
!
interface Ethernet0/0
no shutdown
ip address 192.168.149.100 255.255.255.0
!
1
Download PNETLab Platform
PNETLAB Store
PNETLab.com
line vty 0 4
exec-timeout 0 0
logging synchronous
login local
transport input ssh
You can see that 192.168.149.0/24 is my VMW IP. So you can change it.
First, you need to download netmiko module. You can follow this link:
https://pypi.org/project/netmiko/
Next, I am using Sublimetext Software for Python. You can download from this link:
https://www.sublimetext.com/3
a={
'device_type':'cisco_ios',
'ip':'192.168.149.100',
'username':'cisco',
'password':'cisco123!',
2
Download PNETLab Platform
PNETLAB Store
PNETLab.com
}
sshCli=ConnectHandler(**a)
Step3: Use Netmiko to collect some information
Set a variable to hold the output of the show command you are sending. Use the send_command
function of the sshCli object, which is the SSH session previously established, to send the desired
command. In this case, we are sending sh ip int br.
After sent the command, we would print the result like above.
3
Download PNETLab Platform
PNETLAB Store
PNETLab.com
config_commands = [
'int loopback 1',
'ip add 1.1.1.1 255.255.255.255']
sentConfig = sshCli.send_config_set(config_commands)
print('\n',sentConfig)
And this is the result:
Config_commands is a list command that we want to configure into R1. We use send_config_set
function to enter the configure terminal mode and push all our list command into the router. Let’s
go into R1 check interfac.
R1#show ip int br
Interface IP-Address OK? Method Status Protocol
Ethernet0/0 192.168.149.100 YES manual up up
Ethernet0/1 unassigned YES unset administratively down down
4
Download PNETLab Platform
PNETLAB Store
PNETLab.com