基本的Telnetlib脚本挂起

时间:2018-10-05 00:41:03

标签: python network-programming python-3.6

import getpass
import telnetlib

HOST = "192.168.21.5"
user = input("Enter your remote account: ")
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")

print(tn.read_all().decode('ascii'))

我是python的新手,我认为最好的学习方法就是潜入。我在python标准库中找到了上面的telnetlib代码。我在Ubuntu服务器上使用Python 3.6。我可以从Ubuntu服务器远程登录到路由器,但是当我通过Python运行时,它就挂了。我添加了一个超时值,以查看它是否确实已挂起,并且确定是否足够。我最终打算在路由器上运行命令并以python显示输出。如果在其他主题中都回答过此问题,请共享链接。

这是我运行它后得到的:

tom@ubuntu-server:~/Python_Scripts$ sudo python3.6 telnet3.py
Enter your remote account: tom
Password:
Traceback (most recent call last):
  File "telnet3.py", line 16, in <module>
    print(tn.read_all().decode('ascii'))
  File "/usr/lib/python3.6/telnetlib.py", line 333, in read_all
    self.fill_rawq()
  File "/usr/lib/python3.6/telnetlib.py", line 524, in fill_rawq
    buf = self.sock.recv(50)
socket.timeout: timed out

感谢您的帮助!

0 个答案:

没有答案