3

telnetlib を使用して、最後のコマンドをサーバーに書き込んだ後に出力を出力しています

tn.write(cmd_login)
tn.write(cmd...)
tn.write(cmd_last)
print tn.expect([word],timeout)[-1]

ただし、 return of expected を出力すると、前にサーバーに書き込んだ結果も表示されます(例:cmd_login cmd ...) tn.write(cmd_last) の後にのみ結果を出力する方法はありますか?

4

2 に答える 2

0

各 Telnet.write の後に Telnet.read_until を使用することをお勧めします。read_until の想定される引数には、telnet プロンプトを使用できます。最後に、コマンドを出力から差し引く必要があります。私はより良い方法を知りません。次のようになります。

tn.write(cmd_login)
tn.read_until(prompt)
tn.write(cmd...)
tn.read_until(prompt)
tn.write(cmd_last)
output = tn.read_until(prompt)
cmdIndex = output.find(cmd_last)
output = output[cmdIndex + len(cmd_last):]
print output
于 2013-05-16T07:29:38.693 に答える