28.1.3 Lab - Use The Netmiko Python Module To Configure A Router
28.1.3 Lab - Use The Netmiko Python Module To Configure A Router
28.1.3 Lab - Use The Netmiko Python Module To Configure A Router
Topology
Addressing Table
Device Interface IP Address Subnet Mask
Objectives
Part 1: Build the Network and Verify Connectivity
Part 2: Import Netmiko Python Module
Part 3: Use Netmiko to Connect to the SSH Service
Part 4: Use Netmiko to Send Verification Commands
Part 5: Use Netmiko to Send and Verify a Configuration
Part 6: Use Netmiko to Send an Erroneous Command
Part 7: Modify the Program Used in this Lab
Background / Scenario
With the evolution of the Python language, the netmiko Python module has emerged as an open source
project hosted and maintained on GitHub. In this lab activity, you will use netmiko in a Python script to
configure and verify a router.
Required Resources
1 Router (Cisco 4221 with Cisco IOS XE Release 16.9.4 universal image or comparable)
1 Switch (Optional: any switch available for connecting R1 and the PC)
1 PC (Choice of operating system with Cisco Networking Academy CCNP VM running in a virtual
machine and terminal emulation program)
Ethernet cables as shown in the topology
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
Instructions
enable
configure terminal
hostname R1
no ip domain lookup
line con 0
logging synchronous
exec-timeout 0 0
logging synchronous
line vty 0 15
exec-t 0 0
logg sync
login local
transport input ssh
ip domain name example.netacad.com
crypto key generate rsa modulus 2048
username cisco priv 15 password cisco123!
interface GigabitEthernet0/0/1
description Link to PC
ip address 192.168.1.1 255.255.255.0
no shutdown
ip dhcp excluded-address 192.168.1.1 192.168.1.10
!Configure a DHCP server to assign IPv4 addressing to the CCNP VM
ip dhcp pool LAN
network 192.168.1.0 /24
default-router 192.168.1.1
domain-name example.netacad.com
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
end
copy run start
Close configuration window
student@CCNP:~$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen
1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group
default qlen 1000
link/ether 00:50:56:b3:72:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.15/24 brd 192.168.1.255 scope global dynamic noprefixroute ens160
valid_lft 79564sec preferred_lft 79564sec
inet6 fe80::1ae4:952f:402d:6b1/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group
default qlen 1000
link/ether 00:50:56:b3:26:b6 brd ff:ff:ff:ff:ff:ff
inet 192.168.50.183/24 brd 192.168.50.255 scope global dynamic noprefixroute
ens192
valid_lft 70687sec preferred_lft 70687sec
inet6 fe80::4c87:a2b3:aa9:5470/64 scope link noprefixroute
valid_lft forever preferred_lft forever
c. If the CCNP VM has not received IPv4 addressing, check your physical connections between the host PC
and R1. Also, verify that R1 is configured correctly according to the previous step.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
c. You should be able to login to R1 with the username cisco and password cisco123!. If not, verify that
your SSH configuration is correct on R1.
d. Terminate your SSH session.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
>>>
Step 1: Use the netmiko function send_command to send a command through the SSH session.
a. 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. Notice that the command does not have to be the full
command. It can be any command that the IOS CLI would accept.
output = sshCli.send_command("sh ip int br")
b. Run the program. You will see the following output if your script did not have an error:
================== RESTART: /home/student/netmiko-script.py ==================
>>>
b. The content of the output variable can be made readable with the format option of the print() command.
Also, the “{}\n.” is used here to add a blank line after the output is printed.
print("{}\n".format(output))
c. Run your program now and you should get the following result, which is similar to what you would get
when you enter the command directly into the IOS CLI.
================== RESTART: /home/student/netmiko-script.py ==================
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0/0 unassigned YES unset administratively down down
GigabitEthernet0/0/1 192.168.1.1 YES manual up up
Serial0/1/0 unassigned YES unset administratively down down
Serial0/1/1 unassigned YES unset administratively down down
GigabitEthernet0 unassigned YES unset administratively down down
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
Note: Replace [Student Name] with your name. Leave the \ (backslash) in the description command.
The backslash escapes the apostrophe so that Python does not read it as a closing quote, but as an
apostrophe.
config_commands = [
'int loopback 1',
'ip add 10.1.1.1 255.255.255.0',
'description [Student Name]\'s loopback'
]
b. Create a new variable called sentConfig to hold the results. Then use the send_config_set function of
the sshCli object to send the commands to R1.
sentConfig = sshCli.send_config_set(config_commands)
config term
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int loopback 1
R1(config-if)#ip add 10.1.1.1 255.255.255.0
R1(config-if)#description [Student Name]'s loopback
R1(config-if)#end
R1#
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
Step 1: Create a new loopback interface with the same IPv4 address.
Copy the config_commands variable to the bottom of your netmiko-script.py script and modify it to store a
new loopback interface that uses the same IPv4 address as the previous loopback interface. If multiple
students are accessing R1 at the same time, add 1 to the number of the loopback assigned to you by your
instructor. Otherwise, you can use loopback 2, as shown below.
config_commands = [
'int loopback 2',
'ip add 10.1.1.1 255.255.255.0',
'description [Student Name]\'s loopback'
]
Step 2: Print and format the content of the sentConfig and output variables.
a. Send the commands and print the output like you did for the first loopback interface.
sentConfig = sshCli.send_config_set(config_commands)
print("{}\n".format(sentConfig))
config term
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int loopback 2
R1(config-if)#ip add 10.1.1.1 255.255.255.0
% 10.1.1.0 overlaps with Loopback1
R1(config-if)#description [Student Name]'s loopback
R1(config-if)#end
R1#
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
sshCli = ConnectHandler(
device_type = 'cisco_ios',
host = '192.168.1.1',
port = 22,
username = 'cisco',
password = 'cisco123!'
)
config_commands = [
'int loopback 1',
'ip add 10.1.1.1 255.255.255.0',
'description [Student Name]\'s loopback'
]
sentConfig = sshCli.send_config_set(config_commands)
print("{}\n".format(sentConfig))
config_commands = [
'int loopback 2',
'ip add 10.1.1.1 255.255.255.0',
'description [Student Name]\'s loopback'
]
sentConfig = sshCli.send_config_set(config_commands)
print("{}\n".format(sentConfig))
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 9 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
Note: To find out how the router is configured, look at the interfaces to identify the type of router and how many
interfaces the router has. There is no way to effectively list all the combinations of configurations for each router
class. This table includes identifiers for the possible combinations of Ethernet and Serial interfaces in the device.
The table does not include any other type of interface, even though a specific router may contain one. An example
of this might be an ISDN BRI interface. The string in parenthesis is the legal abbreviation that can be used in
Cisco IOS commands to represent the interface.
end of document
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 9 www.netacad.com