How can you get the SSH return code using Paramiko?

PythonSshParamiko

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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: 
$

Solution 3 - Python

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()

Solution 4 - Python

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: https://stackoverflow.com/questions/107705/disable-output-buffering. For short, just run python with -u option like this:

> python -u script.py

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeyonderView Question on Stackoverflow
Solution 1 - PythonapdastousView Answer on Stackoverflow
Solution 2 - PythonJanCView Answer on Stackoverflow
Solution 3 - PythonperillaseedView Answer on Stackoverflow
Solution 4 - PythonYoungmin KimView Answer on Stackoverflow