Python: (02) Tạo chương trình Telnet bằng Python 3.8 trên CentOS 7

By , 0 View

Mục tiêu thực hành:
- Cấu hình telnet server trên Cisco Router.
- Biên soạn file telnet.py bằng notepad trên Window 10.
- Tiến hành copy file telnet.py lên CentOS bằng WinSCP.
- Tiến hành cài đặt Netmiko API và gói cisco-telnet.git trên CentOS.
- Tiến hành chạy script telnet.py trên CentOS.

Các bước triển khai:
Cấu hình telnet server trên Cisco Router.
hostname Router

enable password cisco
username cisco password cisco

line vty 0 4
 login local
 transport input all
 exit

Biên soạn file telnet.py bằng notepad trên Window 10.
import getpass
import sys
import telnetlib

HOST = "192.168.100.249"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"enable\n")
tn.write(b"cisco\n")
tn.write(b"show arp\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

Tiến hành copy file telnet.py lên CentOS bằng WinSCP.
- Kéo file telnet.py từ trái sang phải để tiến hành copy.

Tiến hành cài đặt Netmiko API và gói cisco-telnet.git trên CentOS.
- Cài đặt Netmiko API.
python3.8 -m pip install netmiko
python3.8 -m pip install --upgrade pip netmiko

- There is a python library on github, specifically for telneting to cisco devices.
pip install git+https://github.com/sergeyzelyukin/cisco-telnet.git

Tiến hành chạy script telnet.py trên CentOS.
[root@localhost Python-3.8.2]# cd /opt/Python-3.8.2
[root@localhost Python-3.8.2]# python telnet.py
Enter your remote account: cisco
Password: 

Router>enable
Password: 
Router#show arp
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.100.237        16   80ce.6282.7f9f  ARPA   Ethernet0/1
Internet  192.168.100.248         5   000c.2978.335f  ARPA   Ethernet0/1
Internet  192.168.100.249         -   aabb.cc00.0510  ARPA   Ethernet0/1
Internet  192.168.100.254         0   e055.3d8e.c978  ARPA   Ethernet0/1
Router#exit

[root@localhost Python-3.8.2]# 

Mọi thắc mắc các bạn vui lòng liên hệ với mình thông qua kênh Zalo (Jade Bùi) 076.877.2021.

You Might Also Like

0 comments