2013-05-01 61 views
2

我使用telnetlib打印的最後一個命令後的輸出寫到服務器我可以清除Telnetlib輸出緩衝器上的Python 2.7

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

然而,當我打印期望的回報,這也顯示我之前寫到服務器的結果(例如:cmd_login cmd ...) 反正只有在tn.write(cmd_last)之後纔打印結果嗎?

回答

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