How can you get the SSH return code using Paramiko?

A much easier example that doesn’t involve invoking the “lower level” channel class directly (i.e. – NOT using the client.get_transport().open_session() command):

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

Leave a Comment