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 の実行前に出力を取得することです -
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