1.Telnetlib模块概述
2.华为ENSP搭建python实验环境
vlanif 1 ip 1923.168.56.x
aaa
local-user admin password cipher Huawei@123 privilege level 3
local-user admin service-type telnet
qu
telnet server enable
user-interface vty 0 4
authentication-mode aaa
protocol inbound all
user-interface console 0
idle-timeout 0
3.Telnetlib案例1:基础语法
基础操作
#导入模块
import telnetlib
import time
#定义telnet用到参数
host = '192.168.56.12'
user = 'admin'
password = 'Huawei@123'
#执行telnet 并输入用户名和密码
#通过类Telnet实例化tn
tn = telnetlib.Telnet(host)
tn.read_until(b"Username:") #b就是把字符变成bytes
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
#提示登录的交换机
print('登录交换机' + host)
#执行配置命令
tn.write(b"sys\n")
tn.write(b"int loopback 1\n")
tn.write(b"ip add 2.2.2.2 32\n")
tn.write(b"quit\n")
tn.write(b"quit\n")
tn.write(b"save\n")
tn.write(b"y\n")
time.sleep(2) #暂停2s,等待设备响应
#打印输出结果
output = (tn.read_very_eager().decode('ascii'))
print(output)
tn.close()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
4.Telnetlib案例2:登录多台设备
import telnetlib
import time
#登录多台设备
hosts = ['192.168.56.11','192.168.56.12','192.168.56.13','192.168.56.14','192.168.56.15','192.168.56.16']
username = 'admin'
password = 'Huawei@123'
for host in hosts:
tn = telnetlib.Telnet(host)
tn.read_until(b'Username:')
tn.write(username.encode('ascii') + b'\n')
tn.read_until(b'Password:')
tn.write(password.encode('ascii') + b'\n')
print('登录设备' + host)
tn.write(b'sys\n')
tn.write(b'vlan 10\n')
tn.write(b'int vlanif 10\n')
tn.write(b'ip add 2.2.2.2 24\n')
tn.write(b'quit\n')
tn.write(b'quit\n')
tn.write(b'save\n')
tn.write(b'y\n')
time.sleep(3)
output = (tn.read_very_eager().decode('ascii'))
print(output)
tn.close()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
5.Telnetlib案例3:循环执行命令
使用for循环登录多台交换机,同时使用for循环执行相关命令
import telnetlib
import time
host = ['192.168.56.11','192.168.56.12','192.168.56.13','192.168.56.14','192.168.56.15','192.168.56.16']
username = 'admin'
password = 'Huawei@123'
commands = ['sys','int loop 0','ip add 1.1.1.1 32','quit','quit','save','y']
for ip in host:
tn = telnetlib.Telnet(ip)
tn.read_until(b'Username:',timeout=1.5)
tn.write(username.encode('ascii') + b'\n')
tn.read_until(b'Password:',timeout=1.5)
tn.write(password.encode('ascii') + b'\n')
print('登录交换机' + ip)
#for循环执行相关命令
for command in commands:
tn.write(command.encode('ascii') + b'\n')
time.sleep(3)
output = (tn.read_very_eager()).decode('ascii')
print(output)
tn.close()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
6.Telnetlib案例4:异常处理
增加异常处理,防止某台交换机登录失败,导致程序中断
import telnetlib
import time
host = ['192.168.56.11','192.168.56.12','192.168.56.13',\
'192.168.56.14','192.168.56.15','192.168.56.16']
username = 'admin'
password = 'Huawei@123'
commands = ['sys','int loop 0','ip add 1.1.1.1 32','quit','quit','save','y']
error = []
for ip in host:
try:
tn = telnetlib.Telnet(ip)
tn.read_until(b'Username:',timeout=1.5)
tn.write(username.encode('ascii') + b'\n')
tn.read_until(b'Password:',timeout=1.5)
tn.write(password.encode('ascii') + b'\n')
print('登录交换机' + ip)
for command in commands:
tn.write(command.encode('ascii') + b'\n')
time.sleep(3)
output = (tn.read_very_eager()).decode('ascii')
print(output)
tn.close()
except:
print('登录失败交换机' + ip)
error.append(ip)
print('登录失败交换机:')
print(error)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
7.Telnetlib案例5:通过文本读取IP和命令
希望非专业人士也能干
import telnetlib
import time
hosts = open('host.txt','r')
username = 'admin'
password = 'Huawei@123'
commands = open('commands.txt','r')
ConnectError = []
AuthenFail = []
for host in hosts.readlines():
try:
#干掉回车或空格
host = host.strip()
tn = telnetlib.Telnet(host)
tn.read_until(b'Username:')
tn.write(username.encode('ascii') + b'\n')
tn.read_until(b'Password:')
tn.write(password.encode('ascii') + b'\n')
index,obj,out = tn.expect([b'Info',b'Error'],timeout=1)
if index == 0:
print('已登录交换机' + host)
elif index == 1:
print('用户名或密码错误' + host)
AuthenFail.append(host)
else:
pass
commands.seek(0)
for command in commands.readlines():
tn.write(command.encode('ascii') + b'\n')
time.sleep(2)
output = (tn.read_very_eager()).decode('ascii')
print(output)
tn.close()
except:
print('连接异常的主机是:' + host)
ConnectError.append(host)
print('-'*100)
print('连接异常的交换机统计如下:')
print(ConnectError)
print('-'*100)
print('认证失败的主机统计如下:')
print(AuthenFail)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
8.Telnetlib案例6:封装成函数解决个例
将telnetlib封装成函数,方便小规模灵活应用,便于调用。同时增加input输入用户名和密码。允许部分交换机用户名和密码不同。
import telnetlib
import time
hosts = open('host.txt', 'r')
username = 'admin'
password = 'Huawei@123'
commands = open('commands.txt', 'r')
ConnectError = []
AuthenFail = []
for host in hosts.readlines():
try:
#干掉回车或空格
host = host.strip()
tn = telnetlib.Telnet(host)
tn.read_until(b'Username:')
tn.write(username.encode('ascii') + b'\n')
tn.read_until(b'Password:')
tn.write(password.encode('ascii') + b'\n')
index,obj,out = tn.expect([b'Info',b'Error'],timeout=1)
if index == 0:
print('已登录交换机' + host)
elif index == 1:
print('用户名或密码错误' + host)
AuthenFail.append(host)
else:
pass
commands.seek(0) #每行读时,光标放到最前面
for command in commands.readlines():
tn.write(command.encode('ascii') + b'\n')
time.sleep(2)
output = (tn.read_very_eager()).decode('ascii')
print(output)
tn.close()
except:
print('连接异常的主机是:' + host)
ConnectError.append(host)
print('-'*100)
print('连接异常的交换机统计如下:')
print(ConnectError)
print('-'*100)
print('认证失败的主机统计如下:')
print(AuthenFail)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.