Use Python To Process and Get Data From Routers On Pnetlab: Function
Use Python To Process and Get Data From Routers On Pnetlab: Function
Use Python To Process and Get Data From Routers On Pnetlab: Function
PNETLAB Store
PNETLab.com
II. Requirement
You are asked to use :
1. Make a Function for telnet to PNETlab device using Pexpect module
2. Make another Function to send command “show ip route “ to Router
3. In Main function, call to these function to telnet to router and send command “show
ip route”.
4. Print output and put the data on a File in your Ubuntu docker.
III. 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.
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
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 5. Create a Directory for Python that it is easy to control python files
mkdir python_on_PNETlab
cd python_on_PNETlab
python3 -m venv env
5
Download PNETLab Platform
PNETLAB Store
PNETLab.com
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
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 : Function_Python
2. Coding in that file.
import pexpect
def connect(dev_ip, username, password):
# "make the function name connect, return with session"
print('You are connecting to PNETlab device:telnet ' + dev_ip)
session = pexpect.spawn('telnet ' + dev_ip, timeout=20)
session.sendline('\r\n') #same with you type the Enter, to start PNETlab telnet.
result = session.expect(['Username:', pexpect.TIMEOUT])
# check for failure
if result != 0:
print('failure')
return 0
print('PNETlab device need Username : username: ' + username)
session.sendline(username)
result = session.expect(['Password:', pexpect.TIMEOUT])
# check for failure
if result != 0:
print('failure')
6
Download PNETLab Platform
PNETLAB Store
PNETLab.com
return 0
print('PNETlab device need password : Password: ' + password)
session.sendline(password)
session.expect('>')
return session
show_ip_route_output = session.before
return show_ip_route_output
# main function to call above functions
interace_list = []
if __name__ == '__main__':
session = connect('10.0.137.221', 'cisco', 'cisco')
if session == 0:
print('failure')
exit()
output_data = show_ip_route(session)
interface_list = output_data.splitlines()
print(interface_list)
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com
session.sendline('quit')
session.kill(0)