“在Python中,支持Telnet/SSH远程登录访问网络设备的模块很多,常见的有Telnetlib、Ciscolib、Paramiko、Netmiko和Pexpect。其中,Telnetlib和Ciscolib对应Telnet协议,后面3个对应SSH协议。”(王印老师《网络工程师的Python之路——网络运维自动化实战》,P124)
安装Telnet及相应服务
检查设备是否已有telnet-server、telnet与xinetd服务:
[root@zabbix-server ~]# rpm -qa telnet-server
[root@zabbix-server ~]# rpm -qa telnet
[root@zabbix-server ~]# rpm -qa xinetd
//如上,没有结果(即未安装)
安装服务:
[root@zabbix-server ~]# yum install -y telnet-server.x86_64
[root@zabbix-server ~]# yum install -y telnet.x86_64
[root@zabbix-server ~]# yum install -y xinetd.x86_64
将Xinetd中的Disable参数修改为no
[root@zabbix-server ~]# vim /etc/xinetd.d/telnet
//没有该文件时则创建该文件,写入以下内容
service telnet
{
disable = yes
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
//注意要将disable的值改为'no'
然后重启服务,并查看23(协议号)端口状态:
[root@zabbix-server ~]# systemctl restart xinetd
[root@zabbix-server ~]# netstat -tnl |grep 23
--------
tcp6 0 0 :::23 :::* LISTEN
--------
//没问题
尝试telnet交换机(本人需要通过CentOS服务器访问数通设备并运行Python脚本写入或修改配置):
又要拿出这张万年拓扑给大家看了:
交换机的管理地址为Vlanif1000 192.168.255.254
[root@zabbix-server ~]# telnet 192.168.255.254
Trying 192.168.255.254...
Connected to 192.168.255.254.
Login authentication
Username:admin
Password:
Info: The max number of VTY users is 5, and the number
of current VTY users on line is 1.
The current login time is 2022-05-08 16:48:09.
<HeXin-SW1>dis version
--------
Huawei Versatile Routing Platform Software
VRP (R) software, Version 5.110 (S5700 V200R001C00)
Copyright (c) 2000-2011 HUAWEI TECH CO., LTD
Quidway S5700-28C-HI Routing Switch uptime is 0 week, 4 days, 23 hours, 30 minutes
--------
<HeXin-SW1>
//注,遇到打错字符无法用Backspace键删除时,用Shift+Backspace即可
//在用户视图下通过'quit'命令回退至Centos界面(依设备而定)
如上,成功访问
CentOS7下telnet的安装参见这篇文章,感谢作者。
图文详细过程Linux 使用yum安装telnet-server、telnet、xinetd以及telnet本机ip_面相对象?的博客-CSDN博客_linux telnet yum
Telnetlib的应用:
“在Python中,我们使用Telnetlib模块来Telnet远程登录网络设备,Telnetlib为Python内建模块,不需要pip下载安装就能直接使用。”(王印老师《网络工程师的Python之路——网络运维自动化实战》,P124)
在王印老师的教程中,老师在远程的设备上开启debug telnet便于后面运行代码时进行验证。王印老师使用的环境为GNS3(Cisco设备),笔者使用的环境为eNSP(华为设备)。
首先开启设备的debug:
<HeXin-SW1>debug telnet
然后写入脚本,脚本功能为远程至某台设备,并为环回口Lo0添加地址。
[root@zabbix-server ~]# touch telnet.py
[root@zabbix-server ~]# vim telnet.py
telnet.py内容如下:
import telnetlib
//通过import语句导入telnetlib模块
host = "192.168.255.254"
user = "admin"
password = "ayanami"
//创建三个变量,对应核心交换机SW1的管理地址、远程用户名、远程密码
tn = telnetlib.Telnet(host)
//调用telnetlib的Telnet()函数,将它赋值给tn,尝试登录这台设备
tn.read_until(b"Username:")
tn.write(user.encode('ascii') + b"\n")
//如果再终端信息里读到"Username:"字样,则通过tn.write(user.encode('ascii') + b"\n")函数输入用户名及回车
//在Python3中使用Telnetlib需要注意如下几点:
在字符串前面要加一个b。
在变量和Telnetlib函数后需要加上.encode('ascii')函数。
在read_all()函数后面需要加上decode('ascii')函数。
tn.read_until(b"Password:")
tn.write(password.encode('ascii')+ b"\n")
tn.write(b"sys \n")
tn.write(b"int lo0 \n")
tn.write(b"ip add 1.1.1.1 24 \n")
tn.write(b"quit \n")
tn.write(b"quit \n")
tn.write(b"quit \n")
//输入命令配置环回口
print (tn.read_all().decode('ascii'))
通过python telnet.py命令来执行该脚本:
[root@zabbix-server ~]# python telnet.py
验证配置是否成功:
[HeXin-SW1]dis cu
......
#
interface LoopBack0
ip address 1.1.1.1 255.255.255.0
#
如上,成功。