How to run script residing on the client on a remote host and get results back to the client in one go.
Asked
Active
Viewed 9,071 times
2 Answers
4
Like this:
ssh host sh -s < script.sh
To redirect remote output to local file:
ssh host sh -s < script.sh > output.txt
Explanation:
ssh host sh
will invoke to default shell on the remote host. The -s
option will tell the remote shell to read commands from standard input. Lastly the redirection < script.sh
will attach stdin
of the remote shell to the local file script.sh
. The last redirection in the second example > output.txt
will attach the stdout
of the remote shell to the local file output.txt
.

Thomas Nyman
- 30,502
I have this file on my client (windows machine running cygwin). I request your help in knowing how to execute this client script on the host and create a log on the host and ship the log back to the windows machine running cygwin (client).
##################
chk_lsnr.sh
rm -f lsnr.exist ps -ef | grep LISTENER | grep -v grep > lsnr.exist if [ -s lsnr.exist ] then echo "Listener is up and running" > lsnr.log else echo "Alert LISTENER DOWN " > lsnr.log fi rm -f lsnr.exist
– Ram Jul 26 '13 at 22:14ssh host sh -s < chk_lsnr.sh && scp host:~/lsnr.log ./lsnr.log
. However, if your script would write the status messages tostdout
instead of to a log file you could use another redirection to write the messages to a local logfile directly:ssh host sh -s < chk_lsnr.sh > lsnr.log
. – Thomas Nyman Jul 26 '13 at 22:21