Command executed with Paramiko does not produce any output
If you get no output on stdout
, it is usually because the command fails to start.
Read stderr
to check for any errors.
print(stderr.readlines())
Quite often the error is "<command> not found". For that see
Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command
If you are connecting to a device, see also Executing command using Paramiko exec_command on device is not working.
A command does not finish when executed using Python Paramiko exec_command
You read only one line of the output.
logging.debug(f'ssh_stdout: {ssh_stdout.readline()}')
If the remote program produces lot of output, as soon as its output buffer fills in, the program hangs on the next attempt to write some output.
If you want the program to finish, you have to keep reading the output.
The simplest way is to use readlines
or read
:
print(stdout.read())
But that's inefficient for large outputs like yours.
Instead you can read the output line by line:
for line in stdout:
print(line.strip())
It gets more complicated, when the commands produces also an error output, as then you have to read both output streams.
See Paramiko ssh die/hang with big output.
And you should check the error output in any case, for good error handling. See also:
Command executed with Paramiko does not produce any output
Python : Paramiko - channel.exec_command not returning output intermittently
It's probably a race condition. The exit_status_ready
can return true if the command has finished, even if you have not read its complete output yet.
The correct code is like:
while True:
exited = channel.exit_status_ready()
if channel.recv_ready():
data = channel.recv(1024)
LOG.debug(data.decode())
while data:
contents.write(data.decode())
data = channel.recv(1024)
LOG.debug(data.decode())
if channel.recv_stderr_ready():
error_buff = channel.recv_stderr(1024)
LOG.debug(error_buff.decode())
while error_buff:
error.write(error_buff.decode())
error_buff = channel.recv_stderr(1024)
LOG.debug(error_buff.decode())
if exited:
break
Paramiko: read from standard output of remotely executed command
You have closed the connection before reading lines:
import paramiko
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
com="ls ~/desktop"
client.connect('MyIPAddress',MyPortNumber, username='username', password='password')
output=""
stdin, stdout, stderr = client.exec_command(com)
print "ssh succuessful. Closing connection"
stdout=stdout.readlines()
client.close()
print "Connection closed"
print stdout
print com
for line in stdout:
output=output+line
if output!="":
print output
else:
print "There was no output for this command"
Related Topics
Downloading a Directory Tree with Ftplib
Python Generator That Groups Another Iterable into Groups of N
How to Create an Empty Array and Then Append to It in Numpy
Difference Between Python3 and Python3M Executables
Simulate Python Keypresses for Controlling a Game
Preprocessing in Scikit Learn - Single Sample - Depreciation Warning
Getting Values from JSON Using Python
Python Selenium: Wait Until Element Is Clickable - Not Working
How to Avoid "Permission Denied" When Using Pip with Virtualenv
How Can a Recursive Regexp Be Implemented in Python
Weighted Choice Short and Simple
Attributeerror: 'Pandasexprvisitor' Object Has No Attribute 'Visit_Ellipsis', Using Pandas Eval
How to Create a Slug in Django
Finding Elements Not in a List
How to Read the Contents of an Url with Python
Finding Elements Not in a List