117
client = paramiko.SSHClient()
stdin, stdout, stderr = client.exec_command(command)

Is there any way to get the command return code?

It's hard to parse all stdout/stderr and know whether the command finished successfully or not.

Charlie
  • 8,530
  • 2
  • 55
  • 53
Beyonder
  • 1,425
  • 3
  • 11
  • 8

4 Answers4

345

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
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
apdastous
  • 3,551
  • 2
  • 12
  • 3
  • 5
    What's *nice* about this example is capturing not just EXIT (like the question asks), but demonstrating you can still get STDOUT and STDERR. Years ago I was misled by Paramiko's anemic example codebase (no disrespect), and to get EXIT I resorted to low-level transport() calls. transport() seemed to force you to "pick one" (which may not be true, but lack of examples and tutorial docs led me to believe that)... – Scott Prive Dec 10 '15 at 18:19
  • 1
    While you are correct about `recv_exit_status`, you cannot use it this way, as the code may deadlock. You have to consume the command output, while waiting for the command to finish. See [Paramiko ssh die/hang with big output](https://stackoverflow.com/q/31625788/850848). – Martin Prikryl Aug 19 '20 at 16:48
54

SSHClient is a simple wrapper class around the more lower-level functionality in Paramiko. The API documentation lists a recv_exit_status() method on the Channel class.

A very simple demonstration script:

import paramiko
import getpass

pw = getpass.getpass()

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect('127.0.0.1', password=pw)

while True:
    cmd = raw_input("Command to run: ")
    if cmd == "":
        break
    chan = client.get_transport().open_session()
    print "running '%s'" % cmd
    chan.exec_command(cmd)
    print "exit status: %s" % chan.recv_exit_status()

client.close()

Example of its execution:

$ python sshtest.py
Password: 
Command to run: true
running 'true'
exit status: 0
Command to run: false
running 'false'
exit status: 1
Command to run: 
$
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
JanC
  • 1,264
  • 9
  • 6
  • 13
    This is a bad solution. See @apdastous' answer below, it's much better. – Patrick Dec 10 '15 at 15:03
  • 1
    While you are correct about `recv_exit_status`, you cannot use it this way, as the code may deadlock. You have to consume the command output, while waiting for the command to finish. See [Paramiko ssh die/hang with big output](https://stackoverflow.com/q/31625788/850848). – Martin Prikryl Aug 19 '20 at 16:48
5

Thanks for JanC, I added some modification for the example and tested in Python3, it really useful for me.

import paramiko
import getpass

pw = getpass.getpass()

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
#client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

def start():
    try :
        client.connect('127.0.0.1', port=22, username='ubuntu', password=pw)
        return True
    except Exception as e:
        #client.close()
        print(e)
        return False

while start():
    key = True
    cmd = input("Command to run: ")
    if cmd == "":
        break
    chan = client.get_transport().open_session()
    print("running '%s'" % cmd)
    chan.exec_command(cmd)
    while key:
        if chan.recv_ready():
            print("recv:\n%s" % chan.recv(4096).decode('ascii'))
        if chan.recv_stderr_ready():
            print("error:\n%s" % chan.recv_stderr(4096).decode('ascii'))
        if chan.exit_status_ready():
            print("exit status: %s" % chan.recv_exit_status())
            key = False
            client.close()
client.close()
perillaseed
  • 119
  • 3
  • 4
  • do we get any error message -if any - in chan.recv_stderr_ready()?. Does chan.recv_stderr(4096).decode('ascii') this return a message text? – kten Sep 07 '17 at 11:24
0

In my case, output buffering was the problem. Because of buffering, the outputs from the application doesn't come out non-blocking way. You can find the answer about how to print output without buffering in here: Disable output buffering. For short, just run python with -u option like this:

> python -u script.py

Youngmin Kim
  • 325
  • 4
  • 12