0

I am executing certain commands and would like to capture the output of the same.

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username="username",password="pwd")
stdin, stdout, stderr = ssh.exec_command("whoami")
out = stdout.read()
print out           # prints username
print stdout.read() # prints nothing, why is it so ?
                    # Instance #1

stdin, stdout, stderr = ssh.exec_command("kill -9 1111")
out = stdout.read()
print out #prints nothing. expected as it doesn't capture if it's successful
# print out # should print "-bash: kill: (1111) - No such process" , if it doesn't exist 
#--> Instance #2

In Instance #1, why print stdout.read() , prints nothing ?

In Instance #2, How to capture such outputs/response ?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

It probably prints nothing because stdout is empty because of an error in the execution. Try printing stderr instead of stdout

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.