Use Python Function To Log On All Network Deviecs To Get Data
Use Python Function To Log On All Network Deviecs To Get Data
PNETLAB Store
PNETLab.com
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 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 :
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
session.sendline(password)
session.expect('>')
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com
return session
#return pexpect session object to caller
def get_inventory(session):
session.sendline('terminal length 0')
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
8
Download PNETLab Platform
PNETLAB Store
PNETLab.com
print ('-------------')
print ('show interface output---inventory')
print (show_int_ouput)
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)
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
10