2

I am writing a Bash Script wherein I need to run some set of commands on a remote Linux server from my local computer using SSH. I want the results to be displayed on the terminal screen or dump the results in a file on my local system.

How can achieve this? Is the following syntax correct?

\#!/bin/bash
.
.
.
.

ssh <user>@<remote_host> 'COMMAND >> /path/to/file ; scp /path/to/file <user>@<local_host>:<location>; exit'
.
.

Thanks in advance.

  • The backslash before #! is certainly not correct. These two characters need to be the very first two bytes in the file in order for them to do anything useful. – tripleee Jul 13 '17 at 17:13
2

To have the results display on the terminal screen:

ssh <user>@<remote_host> COMMAND

To have the results saved to a file:

ssh <user>@<remote_host> COMMAND > FILE

To have the results both displayed on the terminal screen and saved to a file:

ssh <user>@<remote_host> COMMAND | tee FILE
  • Hi Daniel, Thank you for the reply. First command will display the results on terminal screen of remote host. I want it to be displayed on the terminal screen of local machine. – Mandar Shinde Mar 21 '14 at 8:30
  • Since the ssh session is running in the terminal of your local machine, text that is displayed on the remote machine is also displayed on the local machine. I don't believe I understand exactly what you are trying to do. – Daniel S. Mar 21 '14 at 16:00
0

If you do not intervene, stdout will normally appear on the local terminal. It can be saved to a local file as follow:

ssh <user>@<remote_host> COMMAND >my_local_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.