Paramiko Capturing Command Output

Paramiko capturing command output

For your second question: Though I am not specialist of paramiko, I see that function recv, according to the doc, returns a string. If you apply a for loop on a string, you will get characters (and not lines as one might perhaps expect). The newline is caused by your use of the print function as explained on this page, at paragraph 6.3.

I haven't studied what paramiko suggests to do. But why don't you treat the full string as a single entity? For example, you could check the presence of "up" as:

if "up" in output:

Or, if that suits your needs better, you could split the string into lines and then do whatever test you want to do:

for line in output.split('\n'): 

How to capture the output of executed command (python paramiko )?

instance #1

out = stdout.read()
print stdout.read()

The first stdout.read() has already consumed all the output so the second stdout.read() returns nothing.

instance #2

Usually error messages would print to stderr so you should use stderr.read() to get the error.

Reading command output with Paramiko invoke_shell/send/recv never finishes

Your code gets to the point, where the server stops waiting for another input. At the same time you wait for the server to output something. What it never will. That's called a deadlock.

You might have expected some kind of signal that the first command execution has finished. There's no such signal. You are using a "shell" (SSHClient.invoke_shell). The shell is a black box with an input and an output. There are no other signals except for the output, wihhc you are reading already.

The "shell" should not be used to automate command execution. For command automation, there's the "exec" channel (SSHClient.exec_command in Paramiko).

Though I understand that with some special devices, what your server seems to be, you might not have any other option (see Executing command using Paramiko exec_command on device is not working). Moreover I'm not ever sure how the enable command works. Whether it's a command that has finished, or whether it started a kind of a new shell, which is still running and is waiting for subcommands.

So in the end all you can do is to parse the output, waiting for the command prompt (smg-ib-apl008-gen2 [ mgmt-sa ] #).


Yes, it's ugly. You are trying to automate something that was not intended for automation. Maybe your server has a better API then enable shell command, that would be nicer to automate. But that's for another question. From SSH/Python/Paramiko point of view, there's no better solution, if you need to stick with executing the enable command in the shell.


Related questions:

  • How to get each dependent command execution output using Paramiko exec_command
  • Execute multiple dependent commands individually with Paramiko and find out when each command finishes

Though they are about more regular servers, like Linux. So they won't really help. I'm linking them just to provide broader picture and context.

Collect output from top command using Paramiko in Python

The top is a fancy command that requires a terminal. While you can enable the terminal emulation using get_pty argument of SSHClient.exec_command, it would get you lot of garbage with ANSI escape codes. I'm not sure you want that.

Rather, execute the top in batch mode:

top -b -n 1

See get top output for non interactive shell.

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



Leave a reply



Submit