网络工程师的python之路-telnet远程登录设备实验

网络工程师的python之路-telnet远程登录设备实验,第1张

在Python中,我们使用Telnetlib模块来Telnet远程登录网络设备,Telnetlib为Python内建模块,不需要pip下载安装就能直接使用。

Python 3中,Telnetlib模块下所有函数的返回值都变成字节型字符串(ByteStrings)。因此,在Python 3中使用Telnetlib需要注意以下几点:

 • 在字符串的前面需要加上一个b。

• 在变量和Telnetlib函数后面需要加上.encode('ascii')函数。

• 在read_very_eager()函数后面需要加上decode('ascii')函数。

实验拓扑

将ensp的LSW1与本地虚拟网卡loopback0进行桥接,模拟将自己的电脑桥接到以下拓扑网络中。

局域网IP地址段:10.0.1.0 /24;

主机Loopback:10.0.1.10 /24;

LSW2管理ip:10.0.1.20;

LSW3管理ip:10.0.1.21;

LSW4管理ip:10.0.1.22;

LSW5管理ip:10.0.1.23 。

所有交换机都已经预配好了Telnet,用户名为python,密码为123456,用户的权限为15级。

本次实验采用华为ensp模拟器,LSW2~LSW5的配置如下:

#以LSW2为例,配置如下:
sys
sysname LSW2
int vlan 1
ip add 10.0.1.20 24
telnet serv en
user-inter vty 0 4
au mode aaa
aaa
 local-user python password cipher 123456
 local-user python privilege level 15
 local-user python service-type ssh telnet
 #
实验目的:

通过telnetlib模块逐一登录4台交换机LSW2-LSW5,并依次为其配置RSTP,默认模式为MSTP。

实验脚本:

1.引入telnetlib模块和时间模块

import telnetlib
import time

2.定义变量

user='python'
password='123456'

3.调用telnetlib的telnet()函数,将它赋值给tn,尝试以telnet方式登录交换机。

for i in range(20,24):
    host='10.0.1.'+str(i)
    tn=telnetlib.Telnet(host) 

 由于这里有四台交换机,且管理ip地址的前三段市一样的,只是最后一位不一样,且是从20到23,所以我们用for循环,来依次登录这四台交换机。

4.通过tn.read_until('Username:') 的函数来告诉程序:如果在终端信息库读到''Username:''字样,则使用tn.write(user+'\n')函数来输入telnet用户名并回车。

for i in range(20,24):
      host='10.0.1.'+str(i)
      tn=telnetlib.Telnet(host)
      tn.read_until(b'Username:')
      tn.write(user.encode('ascii')+b'\n')

为什么是''Username:'' ,因为在windows系统telnet交换机时,终端会显示Username、Password,如下图所示 :

 5.同理,输入Username后要输入Password,通过tn.read_until('Password:') 的函数来告诉程序:如果在终端信息库读到''Password:''字样,则使用tn.write(password+'\n')函数来输入telnet密码并回车。

      tn.read_until(b'Password:')
      tn.write(password.encode('ascii')+b'\n')
      print('成功登录到交换机:'+host)

6.至此,我们已经让python成功通过Telnet登录交换机。接下来继续用Telnetlib的write()函数在SW1上输入各种配置命令了,这里给交换机配置MSTP。

    tn.write(b'sys\n')
    tn.write(b'stp mode rstp\n')
    #保存配置
    tn.write(b'return\n')
    tn.write(b'save\n')
    tn.write(b'y\n')
    tn.write(b'dis stp | include CIST Global Info\n')
    time.sleep(1)

tn.write(b'dis stp | include CIST Global Info'),是为了查看修改之后的STP模式

这里使用time函数是为了是进程挂起一段时间,以便完整的打印出命令输入时,终端回显消息。

7.打印终端回显消息

      output=tn.read_very_eager().decode('ascii')
      print(output)
      tn.close()#关闭连接

decode就是把二进制数据(bytes)转化成人看的懂得英文或者汉字(decode用的比较多)
encode的作用是将unicode编码的字符串编码成二进制数据 

8.完整代码

import telnetlib
import time
user='python'
password='123456'
for i in range(20,24):
    host='10.0.1.'+str(i)
    tn=telnetlib.Telnet(host)
    tn.read_until(b'Username:')
    tn.write(user.encode('ascii')+b'\n')
    tn.read_until(b'Password:')
    tn.write(password.encode('ascii')+b'\n')
    tn.write(b'sys\n')
    tn.write(b'stp mode rstp\n')
    tn.write(b'return\n')
    tn.write(b'save\n')
    tn.write(b'y\n')
    tn.write(b'dis stp | include CIST Global Info\n')
    time.sleep(1)
    output=tn.read_very_eager().decode('ascii')
    print(output)
    tn.close()
实验结果

点击Run Module运行程序

运行结果 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/728254.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存