3

I have a script that SSH connects from Windows7 to a remote ubuntu server and executes a command. The script returns Ubuntu command output to the Windows cmd window in one go after the command has executed and finished. I am just wondering if there is anyway to return real-time SSH output in my script below, or do I always have to wait for the command to finish before seeing the output.

Here's my working code:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = '9.10.11.12'
port, user, password = 22, 'usr', 'pass'
ssh.connect(host, port,  user, password)
stdin,stdout,stderr = ssh.exec_command("cd /opt/app && ./app-tool some_command")

for line in stdout.readlines():
    print(line)

ssh.close()

Alternatively, if this is not possible with SSH how would I introduce a spinning cursor icon into the above script? Thanks.

0

2 Answers 2

5

Figured it out in the end, I used 'iter' method in the following line:

for line in iter(stdout.readline,""): 
    print(line)
Sign up to request clarification or add additional context in comments.

Comments

0

The output of your command seems to less than the default buffer size because of which it is getting flushed once the command completes.

By default the bufsize is -1 which means that the system default buffer size is used. If bufsize is set to 1 then it is line buffered.

Use

ssh.exec_command("<cmd>",bufsize=1)

3 Comments

Hi SilentMonk, I tried your suggestion but I get the same outcome where the output is dumped at the end of execution.
oh, what about bufsize=0? This means no buffering. Also, what is the size of the output from your command?
Hi SilentMonk, thanks for your help. I had a play around and in the end I used the code >>> for line in iter(stdout.readline,""): print(line)

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.