Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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
share|improve this answer
    
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 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 at 16:00

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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