Telnetlib';s read_直到在Python中才读取预期字符串。什么是替代方案?

Telnetlib';s read_直到在Python中才读取预期字符串。什么是替代方案?,python,timeout,telnet,telnetlib,Python,Timeout,Telnet,Telnetlib,我尝试了几十种不同的应该读取的字符串组合,我尝试了在前后添加空格、拼写和简单的b“”以期望空格,但似乎没有任何东西可以发送第一个命令。之前的其他3个字符串读取得很好。除了此read_,还有什么替代方案 此外,我还尝试了睡眠(.5)和睡眠(1),但都不起作用。我还尝试将字符串写入缓冲区,而不等待读取。救命啊 你试过看这条线吗?-解决此问题的方法是在写入用户名和密码时添加“\r\n”。 因此,您需要使用: tnObject.read_直到(b“用户名:”) tnObject.write(用户+b“\

我尝试了几十种不同的应该读取的字符串组合,我尝试了在前后添加空格、拼写和简单的b“”以期望空格,但似乎没有任何东西可以发送第一个命令。之前的其他3个字符串读取得很好。除了此read_,还有什么替代方案


此外,我还尝试了睡眠(.5)和睡眠(1),但都不起作用。我还尝试将字符串写入缓冲区,而不等待读取。救命啊

你试过看这条线吗?-解决此问题的方法是在写入用户名和密码时添加“\r\n”。 因此,您需要使用: tnObject.read_直到(b“用户名:”) tnObject.write(用户+b“\r\n”) tnObject.read_直到(b“”) tnObject.write(密码+b“\r\n”)

无需使用tnObject。在(b“>”)之前读取

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

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


希望它也能为你工作

你试过看这条线吗?-解决此问题的方法是在写入用户名和密码时添加“\r\n”。 因此,您需要使用: tnObject.read_直到(b“用户名:”) tnObject.write(用户+b“\r\n”) tnObject.read_直到(b“”) tnObject.write(密码+b“\r\n”)

无需使用tnObject。在(b“>”)之前读取

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

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

希望它也能为你工作

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())