【问题标题】:Telnetlib's read_until in Python will not read expected string. What are Alternatives?Python 中的 Telnetlib 的 read_until 不会读取预期的字符串。什么是替代品?
【发布时间】:2016-09-15 02:17:17
【问题描述】:
import getpass
import sys
import telnetlib

host = "10.28.103.126"
user = b"apc"
password = b"apc"
outlet = b"8"

tnObject = telnetlib.Telnet(host)
print("yes")
tnObject.read_until(b"User Name :")
tnObject.write(user + b"\n")
print("sent user")
tnObject.read_until(b" ")
tnObject.write(password + b"\n")
print("sent password")
tnObject.read_until(b"Name ")
tnObject.write(b"1" + b"\n")
print("sent first command")
tnObject.read_until(b">")
tnObject.write(outlet + b"\n")
print("sent second command")
tnObject.read_until(b">")
tnObject.write(b"1" + b"\n")
print("sent third command")
tnObject.read_until(b">")
tnObject.write(b"2" + b"\n") #(1 is ON) (2 is OFF)
print("sent fourth command")
tnObject.read_until(b"Enter ")
tnObject.write(b"yes" + b"\n" + b"\n")
tnObject.read_until(b">")
tnObject.close()

print(tnObject.read_all())

我已经尝试了几十种不同的字符串组合,我尝试过在前后添加空格、拼写和简单的 b"" 来期望空格,但似乎没有任何效果来发送第一个命令。之前的其他 3 个字符串读得很好。这个 read_until 的替代方法是什么?

另外,我尝试过 sleep(.5) 和 sleep(1) ,但也没有用。我还尝试将字符串写入缓冲区而不等待 read_until。救命!

【问题讨论】:

    标签: python timeout telnet telnetlib


    【解决方案1】:

    您是否尝试查看此线程? Python - telnet - automation APC PDU - 解决此问题的方法是在您输入用户名和密码时添加“\r\n”。 所以你需要使用: tnObject.read_until(b"用户名:") tnObject.write(user + b"\r\n") tnObject.read_until(b" ") tnObject.write(密码 + b"\r\n")

    无需使用 tnObject.read_until(b">")

    请注意,对于其他命令,您可以将“\n”留在末尾: tnObject.write(b"userList\n") 应该可以正常工作

    我在我的 APC 系统上尝试过,它成功了。

    希望它也对你有用!

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多