PsyKoptiK
PsyKoptiK

Reputation: 35

How to send stdout of a ssh command in a file?

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.

Upvotes: 1

Views: 3885

Answers (1)

Chen A.
Chen A.

Reputation: 11280

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.

Upvotes: 1

Related Questions