Download PNETLab Platform
PNETLAB Store
PNETLab.com
Use the Netmiko Python Module to
Configure a Router on PNETlab
Lab Topology:
I. Requirement
You are asked to use Netmiko to Configure and check configuration for Routers.
1. Import Netmiko Python Module
2. Use Netmiko to Connect to the SSH Service
3. Use Netmiko to Send Verification Commands
4. Use Netmiko to Send and Verify a Configuration
II. Prerequisite
When you guys load the lab to your PNETlab, it will get all things of this lab except
Ubuntu_server docker. Therefore, you should install by yourself an Ubuntu_server docker and
connect to R2. Now I will show you how to do.
Step 1. Go to Device item of PNETlab site to get Ubuntu_server docker
1
Download PNETLab Platform
PNETLAB Store
PNETLab.com
Step 2. Read the guide to know how to do, or follow my method below.
Step 2.1 After you get Device, then go back to running lab on PNETlab box. Click “Add an
Object” , then Add new Node, then you choose the docker.io. After choosing that box, please
input information follow this picture
2
Download PNETLab Platform
PNETLAB Store
PNETLab.com
Step 2.2 Then you go to Start up configure section. Insert one command line: dhclient eth1
Then save it, enable it. Follow the picture blow.
3
Download PNETLab Platform
PNETLAB Store
PNETLab.com
Step 2.3 Now you have Ubunto_server docker, it is time to connect it to Cloud_NAT
Step 2.4 If you want to add CSR1000 to use in your lab as mine lab. You do the same method
here. That you just get it from “Devices”.
4
Download PNETLab Platform
PNETLAB Store
PNETLab.com
All things related to Ubuntu_server docker are okay. Just click to that device and telnet . Then
just enjoy your Ubuntu in PNETlab now.
Now is the time to check and make Python Environment on Ubuntu. By default, Python3 are
built in your Ubuntu. But to make it work well we should make some check and update or
making the Virtual Environment.
Step 3. Check python version. Make sure it has suitable version. Version 3 is new version of
Python. // Click and telnet to your Ubunto , sodu -i with pass : admin to go to admin level.
admin@Ubuntu_server:~$ python3
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Step 4. Create the virtual environment
sudo apt-get install python3-venv
sudo apt-get install python3-venv
[sudo] password for admin:
Reading package lists... Done
Building dependency tree
Reading state information... Done
// If this command is not Done with this output “E: Unable to fetch some archives, maybe run
apt-get update or try with --fix-missing?”. Please use this command to fix it
apt-get update --fix-missing
Step 5. Create a Directory for Python that it is easy to control python files
mkdir python_on_PNETlab
cd python_on_PNETlab
5
Download PNETLab Platform
PNETLAB Store
PNETLab.com
python3 -m venv env
If problem happen here please use this command again: sudo apt-get install python3-venv
Then execute command again, make sure it is okay now : python3 -m venv env
III. Solution for the lab:
If you don’t familiar with Telnet in Python please practice this lab first : Link Download lab:
https://user.pnetlab.com/store/labs/detail?id=15970569829967
If you already know how to telnet by Python please check the solution for those requirements
1. Nano a python file by any name you want, I choose the name : Python_netmiko
2. When you run program you should use Wireshark to capture packets on Interface of
Routers too. You can do in ASR1000 to see what the process of telnet and getting data
by python.
3. Preconfig for router that you want to ssh to
enable
configure terminal
hostname IOS_XE
no ip domain lookup
line vty 0 15
exec-t 0 0
logg sync
login local
transport input ssh
ip domain name example.pnetlab.com
crypto key generate rsa modulus 2048
6
Download PNETLab Platform
PNETLAB Store
PNETLab.com
username cisco priv 15 password cisco
4. Coding in Python_netmiko file.
=========
from netmiko import ConnectHandler
# '10.0.137.86' is IP of gi1 of ASR1000 router. You can ssh to other IP of other routers.
sshCli = ConnectHandler(
device_type = 'cisco_ios',
host = '10.0.137.86',
port = 22,
username = 'cisco',
password = 'cisco'
)
#1. check interace by command show ip int brief
#output = sshCli.send_command("enable")
output = sshCli.send_command("sh ip int br")
print("{}\n".format(output))
# Create some loopback interace
config_commands = [
'int loopback 1',
'ip add 10.1.1.1 255.255.255.0',
'description loopback 1'
]
sentConfig = sshCli.send_config_set(config_commands)
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com
print("{}\n".format(sentConfig))
output = sshCli.send_command("sh ip interface brief")
print("{}\n".format(output))
config_commands = [
'int loopback 2',
'ip add 10.1.1.1 255.255.255.0',
'description loopback 2'
]
sentConfig = sshCli.send_config_set(config_commands)
print("{}\n".format(sentConfig))
# Then check interface again
output = sshCli.send_command("sh ip int br")
print("{}\n".format(output))
# check wireshark and check result in router and enjoy the lab guys. thanks