我正在使用 Python 的 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 中获得所有合并的输出。
但是,我想要的是在 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