LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2009, 09:51 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
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

Last edited by packets; 05-18-2009 at 09:52 PM. Reason: incomplete post
 
Old 05-19-2009, 12:58 AM   #2
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
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....
 
Old 05-19-2009, 10:12 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 05-19-2009, 04:02 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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.
 
1 members found this post helpful.
Old 05-19-2009, 08:30 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by theNbomr View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 03:35 PM
help in perl how to output on the remote server packets Programming 2 05-11-2009 04:44 AM
SSH - Remote aplications in remote server Urien Linux - Newbie 11 04-04-2009 04:02 AM
SSH remote command withouth output Ricio Linux - Newbie 5 10-20-2008 11:44 AM
Redirect Output to remote PC possible?? wilkija Linux - Newbie 4 10-03-2006 12:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration