telnetlib 类型错误
我正在修改一个 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:预期带有缓冲区接口的对象”任何见解都会有所帮助。
I am modifying a python script to make changes en masse to a hand full of switches via 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")
When the script executes I receive a "TypeError: expected an object with the buffer interface" Any insight would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,
read_until
的规格是(引用,我的重点):在 Python 3 中,您没有传递 byte 字符串,例如:
,您传递的是 text 字符串,这在 Python 3 中意味着统一码字符串。
因此,将其更改为
b"..."
形式是指定文字 byte 字符串的一种方法。(当然对于其他此类调用也类似)。
Per the docs,
read_until
's specs are (quoting, my emphasis):You're not passing a byte string, in Python 3, with e.g.:
Instead, you're passing a text string, which in Python 3 means a Unicode string.
So, change this to
the
b"..."
form is one way to specify a literal byte string.(Similarly for other such calls of course).