How to Read SSH command output without giving delays?

I am executing some Linux command in server using SSHLibrary.
It is successfully executing but the issue is after executing an xyz command, sometimes command takes 1 min or 10 mins to execute/process.

Now I need to fetch the whole output and verify something in it. To read output for now I am giving 10 mins delay in read function of SSHLibrary.

Can I read output of that command without giving any delays in Read? So that it’ll store the output fully even if it is completed in 10min or in 1min without having to wait/idle for max delay which I’ve given.

*** Settings ***
Library    SSHLibrary
Test Setup    Open connection and Log in
Test Teardown    Close all connections

*** Tasks ***
Executing the FML script
    Write    cd /root/config/meta ; Execute script
    ${output}    Read    delay=600s
    Log to Console    ${output}    
    Should Contain    ${output}    <some_string_at_end_of_execution>

Output in terminal and Variable looks like below-
In ${output} I need to store from line-1 to line-600.

    root[user] {acuser} #:
    line 1.......
    line 2.......
    .............
    line 600.....
    root[user] {acuser} #:

I’ve tried using Read Until, Read Until Regexp but not able to get them work.

Please tell me how to resolve this, it’ll save so much of my time while executing robot scripts. Or if you guys have some different approach also please let me know.

Thanks for your help,
Cheers & Happy coding

Hi, you can try with Read Until Prompt which will read until the command prompt symbol is detected (like # as root). Can be customized with Set Client Configuration prompt=${symbol}.

And even better than this, why not still using Read with a smaller delay of let’s say 10s. Since reading is not happening one time, but in cycles until there’s no more output to be retrieved, you’ll wait at most 10 extra seconds past the end of the execution. Make sure there’s at least something outputted in those 10s, otherwise increase the value. (read more on Read)

Also you can try with Read Command Output as well and a return_rc=${True} as it might wait until the just started command run fully ends.

Thanks @cosmin but this method is not working, idk why but read is only reading the output for the time which i’ve given in delay. The working which you defined for read in cycles…that is not working.
Example- If i’ve given read delay=10s, then it is capturing the output for only first 10 seconds, after that nothing is there in variable.

Not working as in output root…# coming twice, so it is taking only first occurrence of prompt.

This I can’t use since I need to execute multiple commands in one session.

Hmm, how about using Read (with a small delay) in a FOR loop until you see there’s nothing coming out as output? (that should definitely work if we misunderstood how the “cycles” thing works on reading)

Worst-case scenario you’ll get back to Read Until Regexp with a better crafted regular expression matching the end of output. (use https://regexr.com/ for testing the regex against an output sample)

Sorry @cosmin!! but still I’m not getting how to use read inside for loop.
If possible can you please give an example considering code which I’ve pasted in the question.

Yes, the better approach is actually a WHILE loop:

SSH Write And Read From Terminal
    Write    ${COMMAND}
    WHILE  ${True}
        ${out} =    Read    delay=1s
        ${size} =    Get Length    ${out}
        IF    ${size} == ${0}    BREAK

        Log To Console    Output: ${out}
    END

How to test

On a Linux machine (with SSH access) I added a Python script that prints for a few seconds:

#! /usr/bin/env python3

import time

for idx in range(1, 5):
    print(idx)
    time.sleep(1)

print("Finished!")

which is runnable with command: cd ~/Scripts && python3 printer.py

And the RF task above outputs every second until you get the following:

1SH Write And Read From Terminal                                      Output:
2
3

Output: 4
Finished!
cmin@linux:~/Scripts$
| PASS |
------------------------------------------------------------------------------
Connect.Tasks :: Test connection related libraries. (SSH)             | PASS |
1 task, 1 passed, 0 failed
==============================================================================
Connect                                                               | PASS |
1 task, 1 passed, 0 failed
==============================================================================
Output:  /Users/cmin/Repos/robots/connect/output/output.xml
Log:     /Users/cmin/Repos/robots/connect/output/log.html
OK.
  • Full robot example
  • SSH Execute And Read Async → a more robust way of executing commands while not waiting for them to finish (then reading their output from last to first – FILO)

Hope the examples should be enough for you. Run them with VSCode or rcc run -t <task>

1 Like

Thanks @cosmin :heart: for explaining, it helped so much.
This is working perfectly the way I wanted it to be.

1 Like