如何使用Paramiko执行多个命令并将输出读取回我的Python脚本?
理论上,这个问题在这里已经有了答案:How do you execute multiple commands in a single session in Paramiko? (Python),但我认为那个答案是不正确的。
问题在于,当你读取stdout时,它会读取终端的整个内容,包括你在终端中“输入”的程序。
只需尝试一下(这基本上是从上面的线程中复制粘贴的):
import paramiko
machine = "you machine ip"
username = "you username"
password = "password"
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(machine, username = username, password = password)
channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')
stdin.write('''
cd tmp
ls
exit
''')
print stdout.read()
stdout.close()
stdin.close()
client.close()
我的问题是,如何执行多个命令并仅读取这些命令的输出,而不是我“输入”的内容和输出?
非常感谢您的帮助和时间。