0

我正在修改一个 python 脚本以通过 telnet 对充满开关的手进行大量更改:

import getpass
import sys
import telnetlib

HOST = "192.168.1.1"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("User Name: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("?\n")
tn.write("exit\n")

当脚本执行时,我收到一个“TypeError:期望一个带有缓冲区接口的对象”任何见解都会有所帮助。

4

1 回答 1

2

根据docsread_until的规格是(引用,我强调):

读取直到遇到预期的给定字节字符串

您没有在 Python 3 中传递字节字符串,例如:

tn.read_until("User Name: ")

相反,您传递的是一个文本字符串,它在 Python 3 中表示一个 Unicode 字符串。

因此,将其更改为

tn.read_until(b"User Name: ")

b"..."表单是指定文字字节字符串的一种方法。

(当然,其他类似的电话也是如此)。

于 2010-03-06T06:30:43.870 回答