telnetlib批量配置交换机时,用for语句第一台完成,之后的就登陆后连接中断,是为什么

以下是我具体问题,由于刚开始学习,谢谢大佬们解答
import telnetlib
import time
login = ‘admin’
password = ‘admin@123’

for x in range(2,4):
print(“telnet to host” + str(x))
host = “192.168.56.” + str(x)
tn = telnetlib.Telnet(host)
tn.read_until(b’Login:’,timeout=(10))
tn.write(login.encode(‘ascii’) + b”\n”)
tn.read_until(b’Password:’)
tn.write(password.encode(‘ascii’) + b”\n”)
time.sleep(2)
tn.read_until(b’‘)
tn.write(b”sys” + b”\n”)
tn.read_until(b”[H3C]”)
tn.write(b”sysname ZZU” + b”\n”)
tn.write(b”vlan 150”+ b”\n”)
tn.write(b”port GigabitEthernet 1/0/2\n”)
tn.write(b”quit\n”)
tn.write(b”int vlan 150\n”)
tn.write(b”no proxy-arp enable\n”)
tn.write(b”ip add 192.168.150.1 255.255.255.0\n”)
tn.write(b”quit\n”)

tn.write(b"end\n")
tn.write(b"exit\n")
time.sleep(3)
print(tn.read_all().decode('ascii'))

tn.close()

host2配置完成,host3就开始报错,报错内容如下:
telnet to host3
Traceback (most recent call last):
File “D:\PycharmProjects\pythonProject\venv\config.py”, line 40, in
tn.read_until(b”[ZZU]”)
File “C:\Users\acer\AppData\Local\Programs\Python\Python37\lib\telnetlib.py”, line 327, in read_until
return self.read_very_lazy()
File “C:\Users\acer\AppData\Local\Programs\Python\Python37\lib\telnetlib.py”, line 403, in read_very_lazy
raise EOFError(‘telnet connection closed’)
EOFError: telnet connection closed

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!