Download PNETLab Platform
PNETLAB Store
PNETLab.com
Use Python on Ubuntu Docker to Telnet
routers of PNETlab.
Lab Topology:
Link Download lab: https://user.pnetlab.com/store/labs/detail?id=15970569829967
Requirement :
You are asked to use Pexpect module to create an Telnet program to log on PNETlab routers.
This is one of the first lab that you try to learn when you start learning automation for network.
=========
I already make the good environment to practice Python on Linux. You guys can check on Linux
docker on PNETlab. But I want to share here that you guys can get the information.
Step 1. Check python version. Make sure it has suitable version. Version 3 is new version of
Python.
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.
1
Download PNETLab Platform
PNETLAB Store
PNETLab.com
Step 2. 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
Step 3. 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
Solution for the lab:
There are many ways to make python on ubuntu docker.
Method 1. Create a python file. Then run this python file and check.
Step 1 . Create Python file and enjoy your python on Ubutu on PNETlab.
nano Telnet_to_PNETLAB_router
import pexpect
HOST = "192.168.80.136 32769"
username="cisco"
password = "cisco"
command='show ip int brief'
session = pexpect.spawn ('telnet '+HOST, timeout=20)
2
Download PNETLab Platform
PNETLAB Store
PNETLab.com
session.sendline('\r\n') # this is special command to enter to new Line. Then we can telnet to
router on PNETlab.
result= session.expect (['Username:', pexpect.TIMEOUT])
session.sendline (username)
result= session.expect (['Password:', pexpect.TIMEOUT])
session.sendline (password)
result= session.expect (['>', pexpect.TIMEOUT])
session.sendline('enable')
result= session.expect (['#', pexpect.TIMEOUT])
session.sendline(command)
result= session.expect (['#', pexpect.TIMEOUT])
session.close()
step 2. Run this python file
admin@Ubuntu_server:~/python_on_PNETlab$ python3 Telnet_to_PNETLAB_router
Traceback (most recent call last):
File "Telnet_to_PNETLAB_router", line 1, in <module>
import pexpect
ModuleNotFoundError: No module named 'pexpect'
admin@Ubuntu_server:~/python_on_PNETlab$ pip3 install pexpect
Collecting pexpect
Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
|████████████████████████████████| 59 kB 743 kB/s
Collecting ptyprocess>=0.5
Downloading ptyprocess-0.6.0-py2.py3-none-any.whl (39 kB)
Installing collected packages: ptyprocess, pexpect
### This mean ubuntu don’t have pexpect module. Just install it by command pip3 install
pexpect.
3
Download PNETLab Platform
PNETLAB Store
PNETLab.com
### now run again.
Method 2. Can run directly by command on Python3 on ubuntu
Step 1. Python3
To access the python by command python3
admin@Ubuntu_server:~/python_on_PNETlab$ 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.
>>>
4
Download PNETLab Platform
PNETLAB Store
PNETLab.com
Step 2. Make Code and enjoy