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
How to Cleanly Uninstall Ansible
Finding the Maximum Number of Columns in a File or CSV Using Python
Retrieving Subfolders Names in S3 Bucket from Boto3
Python | Make the Percentage of a List
How to Convert a Datetime Object to Milliseconds Since Epoch (Unix Time) in Python
Why Is Python No Longer Waiting for Os.System to Finish
Adding Columns to Dataframe Based on File Name in Python
How to Store the Result of an Executed Function and Re-Use Later
How to Delete Comma At the End of the Output in Python
Convert SQL Result to List Python
How to Change the Title Bar in Tkinter
Removing White Space Around a Saved Image
Python 3D Polynomial Surface Fit, Order Dependent
How to Test If a Column Exists and Is Not Null in a Dataframe
How to Remove Comma and Brackets
Filtering Date Column in Python
Loop Over List of Elements for Find_Element_By_Xpath() by Selenium and Webdriver
How to Calculate Rolling/Moving Average Using Python + Numpy/Scipy