client = paramiko.SSHClient()
stdin, stdout, stderr = client.exec_command(command)
有没有办法获取命令返回码?
很难解析所有的 stdout/stderr 并知道命令是否成功完成。
最佳答案
一个更简单的示例,不涉及直接调用“较低级别” channel 类(即 - NOT 使用 client.get_transport().open_session()
命令):
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('blahblah.com')
stdin, stdout, stderr = client.exec_command("uptime")
print stdout.channel.recv_exit_status() # status is 0
stdin, stdout, stderr = client.exec_command("oauwhduawhd")
print stdout.channel.recv_exit_status() # status is 127
关于python - 如何使用 Paramiko 获取 SSH 返回码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3562403/