我将使用Python的telnetlib来telnet到某台机器并执行一些命令,我想要获取这些命令的输出。
目前的情况是这样的:
目前的情况是这样的:
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中获取所有的合并输出。
但是,我想要在command1执行后立即获取其输出,在command2执行之前,就像在另一台机器的shell中一样,如下所示 -
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