我正在使用Python的telnetlib telnet到某臺機器並執行少量命令,我想獲取這些命令的輸出。實時讀取telnetlib的輸出
那麼,究竟目前的情況是 -
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("command1")
tn.write("command2")
tn.write("command3")
tn.write("command4")
tn.write("exit\n")
sess_op = tn.read_all()
print sess_op
#here I get the whole output
現在,我可以得到所有的綜合輸出sess_op。
但是,我想要的是執行後立即命令2的執行之前,如果我在其他機器的外殼正在努力讓Command 1的輸出,如下所示 -
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("command1")
#here I want to get the output for command1
tn.write("command2")
#here I want to get the output for command2
tn.write("command3")
tn.write("command4")
tn.write("exit\n")
sess_op = tn.read_all()
print sess_op
它不工作在我的情況! – theharshest 2012-04-25 10:35:58