Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
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.
I need some help here. I need a help to pipe two commands. The purpose is to execute a command using ssh on other machine.
Say am in server1, I want to run the command as follow
Code:
server1$ssh server2 ls -lt /home/zamo |head -2
What I need is to include the hostname of "server2" also in the same line.
Any idea.I tried with the following , but failed.
Code:
server1$ssh server2 hostname; ls -lt /home/zamo |head -2
If you try to execute a command over ssh, the password prompt stops you from doing that.
You need to create private/public ssh keys without a passphrase. This will allow you to execute remote commands with ssh to a server without being prompted for a password.
Once you have it setup, you execute the command as follows:
The grep part will make that hard because piping to "tr '\n' '\0' after the hostname command will cause the server name and the total line to be on the same line.
There are probably better ways of doing this:
ssh user@host "(hostname; ls -l | head -n 2) | sed -n '1H;/total/d;${H;g;{ s/\n/ /g};p}'"
I needed to change the single quotes to double quotes because the sed command uses single quotes.
These two will give you the output you requested above, everything on one line. If you use the second one enclosed the hostname in back ticks. This will execute the hostname command. If not, it will echo the word hostname.
Its working fine.
I have another question , and i think it will be good to continue that with the same thread, so that you guys can understand it better.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.