2016-05-18 3 views
1
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()) 

私は何十もの異なる文字列の組み合わせを試してみました。スペルを前後に追加しようとしましたが、スペースを期待するためには何もしませんでしたが、最初のコマンド。前の他の3つの文字列は、うまく読めました。このread_untilに代わるものは何ですか?PythonでTelnetlibのread_untilが期待される文字列を読み込まない。代替案とは何ですか?

また、私はsleep(.5)とsleep(1)を試しましたが、どちらもうまくいきませんでした。私はread_untilを待たずにバッファに文字列を書き込もうとしました。助けて!

答えて

0

このスレッドを表示しようとしましたか? Python - telnet - automation APC PDU - この問題が修正された方法は、ユーザー名とパスワードを書き込むときに "\ r \ n"を追加することでした。 だから、あなたが使用する必要があります。 tnObject.read_until(B "ユーザ名:") tnObject.write(ユーザー+ B "\ rを\ n" は) tnObject.read_until(B」「) tnObject.write(パスワード+ B "\ rを\ n" は)

tnObject.read_until(Bを使用する必要はありません ">")他のコマンドのために、あなたが最後に "\ n" を残すことができるという

注: tnObject.write(b "userList \ n")は正常に動作するはずです

私はAPCシステムでそれを試してみました。

あなたもうまくいきますように!

関連する問題