Help with executing multiple remote commands after multiple hops

Hi SSHers,

I have embedded this below code in my shell script..

 
/usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5`

SSH to both these servers are public-key authenticated, so things run smoothly until it comes to the execution of the commands..

It gives me the follow output for the shell script..

ksh: /opt/OV/bin/snmpget:  not found
uptime:

I am assuming that the command '/opt/OV/bin/snmpget' got executed in the second server and since there is no such executable there the error comes up..
Also, in the final target server i get a valid output for the command '/opt/OV/bin/snmpget'..

Could someone help me execute both the commands on the same target server or tell me what i could be doing wrong here?

Thanks In Advance,
Blub:)

I think your issue is a "shell substitution before execution" issue. (See this section 2.1 #4) Try this:

/usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:\\\`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5\\\`

The "snmpget" was executing on $SERVER1. That's where the "snmpget: not found" error came. By using the backslashes, the execution is delayed until access to $SERVER2 is authenticated.

HTH

1 Like

Thanks for your tip, bluescreen.. tried what you had asked me to, but that seems to fail.. Here's the output i got

 
cut: invalid character in range

I will try to change the shell script as per the tips provided in the link..

Wish you a Merry Christmas,
Blub:)..

I got this to work:

$ ssh -t $SERVER1 /usr/bin/ssh $SERVER2 echo uptime: \\\`uptime \\\| cut -d: -f3-5 \\\`

Notice the "\\\" before the pipe. Sorry I forgot that in my previous post. :o

1 Like

Thanks again bluescreen.. Would you by any chance have any link that isnt as detailed as the one you had sent?
I tried another method and implemented the above shell using two shell scripts, one which is a wrapper sitting in the source, the other does the main functionality, and sits in the First hop server.. This seems to work fine..

I will try escaping the pipes and see if that does any good.. Thanks once more mate..

Regards,
Blub:)

i suggest writing simple scripts and then execute the scripts instead of battling with shell metacharacters. it will make your life much easier.

:rolleyes:true, but just wanted to know how to get it working.. thats all:)

create these scripts and make them executable. it should give you a good starting point. add error logic as needed

server1.ksh

#!/usr/bin/ksh

USER2=foo
SERVER2=bar

uptime=$(/usr/bin/ssh $USER2@$SERVER2 /path/to/server2.ksh)
# check $? for ssh errors

echo "uptime: $uptime"

server2.ksh

#!/usr/bin/ksh

nodeName=blah
/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5

then run it

ssh $SERVER1 /path/to/server1.ksh

In my earlier post when i said that i wanted to get it working, i meant, i wanted to know how to get the meta-character thingy working..

Yeah, and this(using 2 scripts) is the other way i implemented my script, and works like a charm, thanks for your help frank..

Wish you a Merry Christmas and a happy new year,
Blub:)

try this.

/usr/bin/ssh -t $USER@$SERVER1 "/usr/bin/ssh $USER2@$SERVER2 'echo -n uptime;/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5'"
1 Like

:b::b:Works nicely.. Could you explain why it wasnt workin earlier and what has been changed in this command?

Thanks a tonne,
Blub:)

it has to do with the quoting and command substitution. using back ticks `` without single quotes was actually causing snmpget to be run on the current system. I simulated the same behavior with echo -n so you don't have to worry about that and also added quotes. it can get tricky and unmaintainable doing things like this.

oks, will take ur advice on this.. THanks for your time, frank..

Regards,
Blub:)

Hey Frank,
I have a smilar problem, where my ssh is not working when I am trying to do the su - user.

ssh -t -f 172.18.197.41 "su - d_od 'ls -lrt /usr/dev/ods|head -10'"

It is working fine if I am not doing the su:

ssh 172.18.197.41 "ls -lrt /usr/dev/ods|head -10"

Can you pls helpme with this.

Thanks
Ajay