2

How to run script residing on the client on a remote host and get results back to the client in one go.

Ram
  • 29

2 Answers2

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
  • The below script I have written to check the status of the listener of a database.

    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:14
  • Something like: ssh host sh -s < chk_lsnr.sh && scp host:~/lsnr.log ./lsnr.log. However, if your script would write the status messages to stdout 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
  • Thomas, Your suggestion worked like a charm!!!! I really Thank You for all the help. – Ram Jul 28 '13 at 03:11
0

Or you could try:

ssh host script.sh
sandyp
  • 1,177