2 - User Netmiko Python To Configure A Router

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5
At a glance
Powered by AI
The key takeaways are learning how to use the Netmiko Python module to connect to and manage a Cisco IOS router via SSH. Netmiko allows sending commands and configurations to the router programmatically.

The purpose of this lab is to learn and understand how to use the Netmiko Python module. Specifically, how to collect information and configure a router through SSH using Netmiko.

To establish an SSH connection using Netmiko, you need to import the ConnectHandler function and pass in parameters like device_type, IP address, username, and password to create a connection object. This object can then be used to send commands to the router.

Download PNETLab Platform

PNETLAB Store
PNETLab.com

Use Netmiko Python to configure a router


Lab Topology:
The lab network topology is illustrated below:

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.

My router have ip that is 192.168.149.100/24. Our ssh account: cisco/ cisco123!

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

After you have done, let’s start:

Step1: Import Netmiko’s ConnectHandler() Function.

from netmiko import ConnectHandler


Step2: The netmiko module includes the ConnectHandler() function. This function requires the
following parameters for establishing an SSH connection with the IOS device:

• device_type - identifies the remote device type

• host - the address (host or IP) of the remote device

• username - remote ssh username

• password - remote ssh password

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

We will try to collect information from command “ show ip int br”

output = sshCli.send_command("sh ip int br")


print(output)
Press Crtl + B in order to run the program, Here is our result:

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.

Step4: Use Netmiko configure a router

Now, we are configuring the Loopback 1 interface on R1:

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

Ethernet0/2 unassigned YES unset administratively down down


Ethernet0/3 unassigned YES unset administratively down down
Loopback1 1.1.1.1 YES manual up up
It is working! Nice!

You might also like