【发布时间】: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