LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   redirect output to remote server via ssh (https://www.linuxquestions.org/questions/programming-9/redirect-output-to-remote-server-via-ssh-726919/)

packets 05-18-2009 09:51 PM

redirect output to remote server via ssh
 
I need to output of the script to the remote server via redirect. I created a simple script for your reference.

Quote:

#!/bin/bash

W=`/usr/bin/w`
FREE=`/usr/bin/free`


echo "$W

$FREE"
I need the output of the script to be transferred to the remote server (192.168.0.1) and do it via ssh or scp. I tried the following:

Quote:

ssh -i /home/foo/test root@192.168.0.1 "/path/scrpt.sh"
I got error when I tried the command above.

Any recommendation.

TIA

Libu 05-19-2009 12:58 AM

A possible solution is create a temp file in your local system and then scp to remote host:


Quote:

#!/bin/bash

W=`/usr/bin/w`
FREE=`/usr/bin/free`


{
echo "$W

$FREE"
} > /path/on/remote/host/file_name 2>&1

scp /path/on/remote/host/file_name root@192.168.0.1:/path/on/192.168.0.1/where/your/want/to/put/the/output/

Just a suggestion, its never a good idea to scp to another host as user root. Security consideration et all....

theNbomr 05-19-2009 10:12 AM

IO redirection works on files. If the file to which you redirect the stdout or stderr is mounted on the local filesystem, and if it is writable, then IO redirection will work as usual. To make the output file exist on a remote host, that remote host's filesystem must be exported by the remote host, and then mounted on the local filesystem. The two most likely candidates for performing this would be NFS or SMB/CIFS. There is no magic way to redirect output to an arbitrary remote filesystem. Libu's suggestion of creating the file on the local filesystem, and then transferring it to the remote host (which can be done in any number of ways), would be your other alternative.
--- rod.

osor 05-19-2009 04:02 PM

I am not sure I understand the question. I cannot tell if you want the output of the script to be redirected to a file on the remote host, or if you want the output of the script to be echoed as commands to be executed. Both are possible, but I assume you mean the former.

In this case, it is really a simple matter of using a pipe to redirect to ssh, followed by shell redirection on the remote machine. For example,
Code:

echo "$(/usr/bin/w)

$(/usr/bin/free)" | ssh root@192.168.0.1 "sh -c 'cat > /where/your/want/to/put/the/output'"

P.S.
As suggested above, ssh to root is a bad idea all around.

ntubski 05-19-2009 08:30 PM

Quote:

Originally Posted by theNbomr (Post 3545872)
To make the output file exist on a remote host, that remote host's filesystem must be exported by the remote host, and then mounted on the local filesystem. The two most likely candidates for performing this would be NFS or SMB/CIFS.

sshfs would be the obvious choice here, I think.


All times are GMT -5. The time now is 03:01 AM.