LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Send output of command to a different server (https://www.linuxquestions.org/questions/linux-newbie-8/send-output-of-command-to-a-different-server-4175600424/)

NotionCommotion 02-23-2017 08:01 AM

Send output of command to a different server
 
How can I perform the following in one command? If not one, what about two?

Code:

# Currently logged on server 999.999.999.999
/path/to/command 1234 > /path/to/file
scp /path/to/file username@111.111.111.111:/path/to/destination
rm /path/to/file

Same question, but if logged on to 999.999.999.999 instead of 111.111.111.111

nodir 02-23-2017 08:04 AM

Try something like
command 1234 | ssh user@remote_host "cat >> file_name"

NotionCommotion 02-23-2017 08:16 AM

Thanks nodir,

So, run command 1234,pipe it to sshing into remote host, and cat'ing the results to file_name? Why >> instead of >?

Also,what if I was currently logged on remote_host?

nodir 02-23-2017 08:21 AM

i copied the command from a script where i want to append to a textfile.
Hence the >> instead of a >
(in short: a typo. sorry :-).

And yes, i guess you gave the general summary of it (the quotes around " cat > filename" do matter, if i recall correct).
ssh command
only executes the command remote, after that you are still local (in case that was a question)


Not sure about the other question, user is logged in remote, i guess it doesn't matter.

good luck.

BW-userx 02-23-2017 09:20 AM

> as apposed to >>

> writes to the file if something is already there is gets written over will the new stuff.
>> appends whatever is being sent to what is already there.

using >> on first write will still get a file written, just the next time you will not have to change the > to >> if you are keeping a log type file, it saves recoding.

Using nodir example with a little change to path to directory first.
Code:

command 1234 | ssh user@remote_host "cat >> /path/to/dir/file_name"
but as his name suggest he has no directory so maybe that is why he did not included a path to a directory in his example. (just playing off of your user name nodir. Nothing personal) :)

frieza 02-23-2017 09:45 AM

that would work, the question I have is, do you have root access to these machines? If so could you use an NFS share? or fuse SSHFS to mount the remote directory locally and then just read/write the file/directory as if it were local? Just a thought.

r3sistance 02-23-2017 10:02 AM

rsync would be better, it already has the ability to handle this with the --remove-source-files flag

thinking something like

Code:

/path/to/command 1234 > /path/to/file && rsync -avz --remove-source-files /path/to/file user@host:/destination/path


All times are GMT -5. The time now is 10:44 PM.