28.1.3 Lab - Use The Netmiko Python Module To Configure A Router - ITExamAnswers
28.1.3 Lab - Use The Netmiko Python Module To Configure A Router - ITExamAnswers
(Instructor Version)
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
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)
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
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
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
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
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 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
)
c. Save the script and run it. You will see the following output if your script did not have an error:
================== RESTART: /home/student/netmiko-script.py ==================
>>>
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 ==================
>>>
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
Instructor Note: If you are in a lab environment with multiple student completing this lab at the same time,
use a scheme to assign students a unique loopback interface and IPv4 address. For example, you could use
a table like the following. Add as many loopback addresses as you need.
blank
10 10.10.1.1/24
Blank
12 10.12.1.1/24
Blank
14 10.14.1.1/24
blank
16 10.16.1.1/24
config term
Enter configuration commands, one per line. End with CNTL/Z.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
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#
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
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 11 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',
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
sentConfig = sshCli.send_config_set(config_commands)
print("{}\n".format(sentConfig))
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
Router R1
R1# show run
Building configuration...
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 11 www.netacad.com
Lab - Use the Netmiko Python Module to Configure a Router
interface Serial0/1/0
no ip address
!
interface Serial0/1/1
no ip address
!
ip forward-protocol nd
no ip http server
ip http secure-server
!
control-plane
!
line con 0
exec-timeout 0 0
logging synchronous
transport input none
stopbits 1
line aux 0
stopbits 1
line vty 0 4
exec-timeout 0 0
logging synchronous
login local
transport input ssh
line vty 5 15
exec-timeout 0 0
logging synchronous
login local
transport input ssh
!
end
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 11 www.netacad.com