1

I'd like to send the output the stdout of a ssh command in a file but this file remains empty when I read it.

# ssh -o "BatchMode=yes" -o "ConnectTimeout=5" [email protected] > /var/tmp/.result.txt
ssh: connect to host 10.10.10.10 port 22: Connection timed out
# cat /var/tmp/.result.txt
#

How to do so? Thanks.

5
  • 1
    Your connections fails, and what you see is the error output. What output you want it to redirect? Commented Oct 31, 2017 at 8:39
  • Oops, your answer made me realize that I have to send the stderr! Actually I wanted to send the error output in my file... :) Commented Oct 31, 2017 at 8:41
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Commented Oct 31, 2017 at 8:52
  • @jww this command could well form part of a shell script so I don't see how it is off-topic. Commented Oct 31, 2017 at 9:27
  • @arco444 - As written its just another question asking for help with an unrelated command. There are more appropriate sites for those questions. I'm also aware of the "in a script" trick to try to bring them back on topic. I'm usually not swayed unless the code of a real script is offered. Commented Oct 31, 2017 at 9:34

1 Answer 1

1

If you want to pass regular stdout to a file, your command does that. Your issue is you get an error; and this is printed to stderr. To pass stderr you need to add this to your command

ssh -o "BatchMode=yes" -o "ConnectTimeout=5" [email protected] > /var/tmp/.result.txt 2>1&

This tells bash to redirect output to .results.txt, and then redirect stderr to stdout, so they both are printed to the file. It reuses the file descriptor which stdout uses.

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

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.