Download PNETLab Platform
PNETLAB Store
PNETLab.com
USE PYTHON FUNCTION TO LOG ON ALL NETWORK
DEVIECS TO GET DATA
Lab Topology:
I. Requirement
You are asked to use :
Log on in all Devices of Your Network to Get Inventory.
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
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
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_Function_Multi_Devices
2. Coding in that file.
import pexpect
def read_all_device(filename):
devices_list=[] #list format
file=open(filename,'r')
for line in file:
device_list_info=line.strip().split(',')
device_info = {}
device_info['name']=device_list_info[0]
device_info['ip']=device_list_info[2]
device_info['username']=device_list_info[3]
device_info['password']=device_list_info[4]
devices_list.append(device_info)
return devices_list
6
Download PNETLab Platform
PNETLAB Store
PNETLab.com
output=read_all_device('PNETlab_devices')
print (output) # to check result in your screen
def connect(dev_ip,username,password):
print ('you are using function to connecting to PNETlab', dev_ip) # don't have ‘+’ here
session=pexpect.spawn('telnet ' + dev_ip, timeout=20)
session.sendline('\r\n') #special action like Enter, juse use it in PNETlab platform , in your
real network no need this. But it is nice.
result=session.expect(['Username:', pexpect.TIMEOUT])
#check for failure
if result !=0:
print ('failure')
return 0
print ('connecting to PNETlab : username: ' +username)
session.sendline(username)
result=session.expect(['Password:', pexpect.TIMEOUT])
#check for failure
if result !=0:
print ('failure')
return 0
print ('connecting to PNETlab : Password: ' +password)
session.sendline(password)
session.expect('>')
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com
return session
#return pexpect session object to caller
#define function using Session above to gets device information (inventory)
def get_inventory(session):
session.sendline('terminal length 0')
result=session.expect('>')
session.sendline('show interface summary')
result=session.expect('>')
get_inventory_out=session.before
return get_inventory_out
# if we call this function (get_inventory) we will get the output of get_inventory_out
# make a function to print the device info
def print_device_info (device_info,show_int_output):
print ('PNETdevice Name: ',device_info['name'])
print ('PNETdevice IP: ',device_info['ip'])
print ('PNETdevice Username: ',device_info['username'])
print ('PNETdevice Password: ',device_info['password'])
8
Download PNETLab Platform
PNETLAB Store
PNETLab.com
print ('-------------')
print ('show interface output---inventory')
print (show_int_ouput)
#========now is main program
if __name__ =='__main__':
devices_list=read_all_device('PNETlab_devices')
for device_info in devices_list:
session = connect(device_info['ip'],
device_info['username'],
device_info['password'])
if session == 0:
print ('Tellneting to PNETlab devices failed')
continue
show_int_ouput = get_inventory (session)
Output_inventory = print_device_info (device_info,show_int_ouput)
session.sendline('quit')
session.kill(0)
#send to a file in computer
file= open('Python_function_Inventory_PNETlab','w+')
file.write(str([show_int_ouput]))
file.close()
9
Download PNETLab Platform
PNETLAB Store
PNETLab.com
=====
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.
10