Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-23-2017, 08:01 AM
|
#1
|
Member
Registered: Aug 2012
Posts: 789
Rep:
|
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
|
|
|
02-23-2017, 08:04 AM
|
#2
|
Member
Registered: May 2016
Posts: 222
Rep:
|
Try something like
command 1234 | ssh user@remote_host "cat >> file_name"
|
|
|
02-23-2017, 08:16 AM
|
#3
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep:
|
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?
|
|
|
02-23-2017, 08:21 AM
|
#4
|
Member
Registered: May 2016
Posts: 222
Rep:
|
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.
Last edited by nodir; 02-23-2017 at 08:23 AM.
|
|
|
02-23-2017, 09:20 AM
|
#5
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
> 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)
Last edited by BW-userx; 02-23-2017 at 09:23 AM.
|
|
|
02-23-2017, 09:45 AM
|
#6
|
Senior Member
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233
|
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.
|
|
|
02-23-2017, 10:02 AM
|
#7
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
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
Last edited by r3sistance; 02-23-2017 at 10:15 AM.
|
|
|
All times are GMT -5. The time now is 10:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|