SSH, Remote Commands and echo, oh my!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH, Remote Commands and echo, oh my!
# 1  
Old 07-15-2009
Java SSH, Remote Commands and echo, oh my!

So, HostB has a SSH trust via pre-shared keys from HostA.

HostA> ssh HostB hostname
HostB

HostA> ssh HostB echo `hostname`
HostA

HostA> ssh HostB 'echo `hostname`'
`hostname`

HostA> ssh HostB "echo `hostname`"
HostA

HostA> ssh HostB echo $PS1
user@HostA:$PWD

HostA> ssh HostB 'echo $PS1'
$PS1


I'm at a loss for how to get these types of commands to run remotely. Echo seems to always interpret locally.

sshd_config is setup for PermitRootLogin forced-commands-only.

The authorized_keys is setup for command=$SSH_ORIGINAL_COMMAND ssh-dss <key> user@HostA

Just including that in case it's somehow causing my grief, though it seems more like quoting, trapping, etc.

The idea is this will be used on many systems to run a command, and it'd be nice to be able to prefix returns w/ a hostname for example.

Locally, I could run:

echo "`hostname`: `df -g /tmp|tail -1`"

and get

HostA : /dev/hd3 16.00 4.06 75% 7655 1% /tmp

So that way, if I script to run against a large amount of hosts, I get a hostname prefix for easy output reading.

Though the redirect also brings up another question.

If I do

HostA> ssh HostB cat /etc/passwd|grep user
user::100:100:<snip>

Is the grep happening remotely, or is the grep happening locally after the data is returned?

Thanks in advance for any insight you can offer.
# 2  
Old 07-16-2009
Try this:

Code:
for server in "host1" host2" host3" ;

do
   ssh $server /usr/local/apache/bin/apachectl graceful
 done

Of course change the command to what ever you want. I am showing how to gracefully restart apache on the remote hosts. You can substitute that command with yours.

Jaysunn

Last edited by jaysunn; 07-16-2009 at 04:07 PM.. Reason: Also you can save this in a script.sh and make it executable. chmod +x script.sh
# 3  
Old 07-20-2009
#2

Thanks for the reply.

Yes, I knew I could for loop commands, but the issue was trying to understand why echo is being interpreted locally.

What I've done in the mean time is write a sshpush wrapper that just pushes a script file remotely, executes it, then cleans it up afterwards.

It's not quite as nice IO wise, cause it takes a few scp/ssh commands to make it work, but it gets the job done. Can't seem to command stack via ssh delivered commands either:

ssh HostA hostname;hostname
Changes the remote hostname, and that's bad.

Where the expected output would be:
HostA
HostA

And yes, even if you trap the semi-colon, it just changes your hostname to \;
# 4  
Old 07-20-2009
its because everything inside of the `'s are executed before leaving your host.

just do the ssh host command

Code:
risk@Heavyarms [164] --> ssh zhilliard@xxxxxxx hostname;hostname
zhilliard@xxxxxxx's password:
li27-239
Heavyarms
-(~/work/sun6kchassis/working)------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(15:19 Mon Jul 20)
risk@Heavyarms [165] -->

# 5  
Old 07-20-2009
This is because your system is resolving the variable before it sends the command so:

Code:
HostA# ssh HostB "echo $HOSTNAME"

is sending the command "echo HostA" to HostB, which it is doing. One thing you can do is put a backslash before the dollar sign:

Code:
HostA# ssh HostB "echo \$HOSTNAME"

This will send the command "echo $HOSTNAME" to HostB. This doesn't work with single quotes though. I got around it recently by doing this:

Code:
remotecmd=      "echo -n 'The hostname of this computer is ';
               echo \$HOSTNAME"
ssh HostB $remotecmd

which would return:

Code:
The hostname of this computer is HostB

# 6  
Old 07-20-2009
or..

Code:
-bash-3.2$ ssh -l myid host2 "echo \`hostname\`"
host2
-bash-3.2$ hostname
host1
-bash-3.2$

# 7  
Old 07-20-2009
Quote:
Originally Posted by ryandegreat25
or..

Code:
-bash-3.2$ ssh -l myid host2 "echo \`hostname\`"
host2
-bash-3.2$ hostname
host1
-bash-3.2$


why would you not just do ssh id@server hostname

Code:
[risks-macbook] ~ > hostname
risks-macbook
[risks-macbook] ~ > ssh risk@xxxx.com hostname
risk@xxxx.com's password: 
li27-239
[risks-macbook] ~ > ssh risk@xxxx.com hostname
risk@xxxx.com's password: 
li27-239
[risks-macbook] ~ >

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Commands not working with ssh remote login

Hi Friends, I am unable to run our application commands on remote server using ssh (passwordless login enabled). But the same command running with telent perl script. please suggest. SSH: C:/bin>ssh -l monitor tl04cp01 exec "/home/monitor/123" /home/monitor/123: viewlog: not found. ... (7 Replies)
Discussion started by: suresh3566
7 Replies

2. Shell Programming and Scripting

Ssh - running commands on remote server

Hi, I am trying to run commands on a list of servers that I can ssh to and just want to know if there is a 'cleaner' way of doing this. At the moment, I am doing as below. Is there a way that I can escape the double quote differently? If a use a single quote to enclose the commands that I... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

Running Commands on a Remote Linux Server over SSH

Hello, I'm trying to create a ksh script to ssh to a remote server, enter the password and a couple commands. For security reasons I have changed the login, password and ip in my example. #!/bin/ksh ssh -t -t username@12.5.5.3 << EOF password cd bin pwd EOF When I run it. It... (5 Replies)
Discussion started by: seekryts15
5 Replies

4. Shell Programming and Scripting

Remote ssh commands

ssh XXXXX@XXXXX'. $HOME/.profile 2>/dev/null;cd $COMMON_TOP/admin/scripts/$CONTEXT_NAME; adcmctl.sh start "apps${AppsPass}" || cd $ADMIN_SCRIPTS_HOME; adcmctl.sh start "apps${AppsPass}"' As per our business requirement we should be able to execute the above two commands seprated with or ( || )... (4 Replies)
Discussion started by: Y.balakrishna
4 Replies

5. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

6. Shell Programming and Scripting

Executing remote commands via ssh

Hi, I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen. # /usr/bin/bash ls $1 > /dev/null 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $server ... (5 Replies)
Discussion started by: moshesa
5 Replies

7. Shell Programming and Scripting

executing commands in remote server using ssh

I have some commands which need to be executed in remote machine. I have Linux Server from where I need to connect to Solaris server using ssh and then declare some variable over there and run some commands. I don't want to call a script which is present in Solaris server from Linux server... (7 Replies)
Discussion started by: maitree
7 Replies

8. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

9. Shell Programming and Scripting

Send Remote Commands via SSH with variables

Hi there I found the Command to send commands to other servers like: sv01> ssh user@sv02 'ps -ef' But I cant use Variables from a script i want to execute on another server like: sv01> ssh user@sv02 'cd $SCRIPTHOME' although the variable is set on sv01. How can I run commands on sv02 with... (2 Replies)
Discussion started by: DarkSwiss
2 Replies

10. Shell Programming and Scripting

remote ssh commands help

I have a script that I'm trying to use that will check for files on a remote machine and if there is files copy the files, modify the perm on the copied files, and then delete the files off of the remote server. Right now I need to get the correct syntax so the the remote commands still interpret... (4 Replies)
Discussion started by: jcalisi
4 Replies
Login or Register to Ask a Question