我正在尝试通过Python Telnet代码Telnet到节点,但是这需要我输入密码,然后挂起,或者出现错误消息:“管道损坏(err 32)”或“套接字超时(err 61)” 如果一切顺利的话。输入密码后,代码会挂起很长时间,并显示“超时”错误。我不确定是什么问题。请帮助我
我尝试捕获异常并更改端口。 我尝试了Python Idle和命令提示符,但是代码不起作用 我可以通过终端手动登录到节点,而无需输入代码
import getpass
import sys
import telnetlib
HOST = "135.250.64.125"
user = input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST,5024)
tn.read_until("login: ")
tn.write(user + "")
if password:
tn.read_until("Password: ")
tn.write(password + "")
# tn.write("ls ")
#
# tn.write("exit")
print( tn.read_all())
要登录到节点并获得终端输出
我得到的错误
Enter your remote account: "admin"
Password:
Traceback (most recent call last):
File "telnet", line 11, in <module>
tn = telnetlib.Telnet(HOST,8080, 5)
File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py", line 211, in __init__
self.open(host, port, timeout)
File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py", line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection
raise err
socket.error: [Errno 61] Connection refused
Enter your remote account: "admin"
Password:
Traceback (most recent call last):
File "telnet.py", line 17, in <module>
tn.write(user + "")
File "/usr/lib64/python2.7/telnetlib.py", line 282, in write
self.sock.sendall(buffer)
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 32] Broken pipe