Python Telnet开关连接已关闭

时间:2018-08-28 19:05:40

标签: python networking telnet

对于使用python远程登录到交换机,我是陌生的,因此,如果我要提出明显的要求,请原谅。我在Python 3.6中使用了以下脚本,但看来telnet连接已关闭。我已经能够在命令提示符下通过telnet正确连接,所以我不确定脚本的运行情况。

import getpass
import sys
import telnetlib
import time

Host="192.168.43.10"

user=input(“Enter User name”)
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”)
time.sleep(2)
tn.write(b”enable\n”)
time.sleep(2)
tn.write(b”admin\n”)
time.sleep(2)
tn.write(b”config t\n”)
time.sleep(2)
tn.write(b”int vlan 10\n”)
time.sleep(2)
tn.write(b”ip add 10.10.10.10 255.255.255.0\n”)

tn.write(“end\n”)
tn.write(“exit\n”)
line=tn.read_all()
 print (line)

但是,输入用户名和密码后,出现以下错误:

"C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\telnetlib.py", line 327, in read_until
    return self.read_very_lazy()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\telnetlib.py", line 403, in read_very_lazy
    raise EOFError('telnet connection closed')
EOFError: telnet connection closed
>>> 

0 个答案:

没有答案