Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Download PNETLab Platform

PNETLAB Store
PNETLab.com

USE PYTHON FUNCTION TO PROCESS AND GET DATA


FROM ROUTERS ON PNETlab.
I. Lab Topology:

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 1. Go to Device item of PNETlab site to get Ubuntu_server docker

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 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
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

IV. 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 : 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

# return the Session to caller in next function.


def show_ip_route(session):
print('show ip route')
session.sendline('terminal length 0')
result = session.expect('>')
session.sendline('show ip route')
result = session.expect('>')
print('getting command output')

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)

#to create the file in Ubuntu.


file= open('Python_function_on_PNETlab','w+')
file.write(str([intf_routes]))
file.close()
=====
When you run python3 Function_Python if you meet the fault below, you should install
module pexpect

root@Ubuntu_server:~/python_on_PNETlab# python3 Function_Python


Traceback (most recent call last):
File "Python_Expression", line 1, in <module>
import pexpect
ModuleNotFoundError: No module named 'pexpect'
root@Ubuntu_server:~/python_on_PNETlab# pip3 install pexpect
After that you can run Python again.

You might also like