LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-30-2008, 09:23 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
To execute two commands in a single line remotely


Hi all,

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
Here the second command fails.

Last edited by ZAMO; 10-30-2008 at 10:17 PM.
 
Old 10-30-2008, 09:40 AM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Works on my machine with hosts file. also works with $ ssh myuser@server2.domain ls |grep filename That may work for you.

Dave
 
Old 10-30-2008, 11:03 AM   #3
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

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

ssh user@server2 'ls -lt /home/zamo |head -2'

http://www-128.ibm.com/developerwork...ry/l-keyc.html

This website shows how to create ssh keys for authentication. It also explains the risk of logging into a ssh server without a password.

Last edited by dv502; 10-30-2008 at 11:17 AM.
 
Old 10-30-2008, 01:27 PM   #4
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Sorry, dv502 is correct, I had assumed you were using pub/pri keys.
Dave
 
Old 10-30-2008, 10:20 PM   #5
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you both.

Am using Pub key authentication and the trust is there with both the servers. (no passwd required).

I am trying to know , how to execute 2 commands in a single line in ssh like we do on a local shell.

Code:
 hostname; ls -lt /home/zamo
 
Old 10-30-2008, 10:24 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Put your oneliner in single quotes so that it is a single argument to ssh.
 
Old 10-30-2008, 11:02 PM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thanks GURU,

Its working fine. Another short question by the way .
The command
Code:
server1$ssh server2 'hostname; ls -lt /home/zamo |grep -v total|head -1'
give the output as follows;
Code:
server2
drwxrwxr-x  2 zamo zamo  4096 Oct 30 04:04 newfile
Is there a way to print the output as

Code:
server2 drwxrwxr-x  2 zamo zamo  4096 Oct 30 04:04 newfile

Last edited by ZAMO; 10-30-2008 at 11:04 PM.
 
Old 10-30-2008, 11:18 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 10-30-2008 at 11:20 PM.
 
Old 10-30-2008, 11:25 PM   #9
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Code:
'echo -ne $HOSTNAME \\t ; ls -lt /home/zamo |grep -v total|head -1'
or
Code:
'echo -ne `hostname` \\t ; ls -lt /home/zamo |grep -v total|head -1'
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.

Hope this helps...

Last edited by dv502; 10-31-2008 at 12:15 AM.
 
Old 10-31-2008, 12:19 AM   #10
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thanks dv502,

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.

In
Code:
'echo -ne $HOSTNAME \\t ; ls -lt /home/zamo |grep -v total|head -1'
The hostname outputs server2.mynetwork.com. I just want the server2 only.
So am trying with
Code:
'echo -ne $HOSTNAME |awk -F\. '{print $1F}'  ; ls -lt /home/zamo |grep -v total|head -1'
and it is working. To bring the output in the same line am try with
Code:
'echo -ne $HOSTNAME |awk -F\. '{print $1F}' \\t ; ls -lt /home/zamo |grep -v total|head -1'
for that am getting the error
Code:
awk: cmd. line:2: fatal: cannot open file `t' for reading (No such file or directory)
Is there a way (or any alternate)to use \\t after awk -F\. '{print $1F}'

Last edited by ZAMO; 10-31-2008 at 12:28 AM.
 
Old 10-31-2008, 01:00 AM   #11
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Have you tried the second one?

Code:
'echo -ne `hostname` \\t ; ls -lt /home/zamo |grep -v total|head -1'
 
Old 10-31-2008, 01:26 AM   #12
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Code:
'echo -ne `hostname` \\t ; ls -lt /home/zamo |grep -v total|head -1'
is working
 
Old 10-31-2008, 01:31 AM   #13
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
Glad it work
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby execute commands remotely Dr_Death_UAE Programming 0 08-16-2008 08:47 PM
from Win XP, remotely execute a command on Linux using ssh lothario Linux - Software 3 06-21-2008 05:59 PM
how to remotely execute linux and windows program alix123 Linux - Software 8 05-14-2007 04:12 PM
URGENT: How to execute a Linux command remotely alix123 Programming 7 04-06-2006 03:00 AM
Using FTP to execute command line commands remotely steve_2010 Solaris / OpenSolaris 4 02-13-2004 08:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:57 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