views:

38

answers:

2

Hi

I am trying to send a command over ssh with a parameter but the shell fails to expand the command properly any ideas what am i doing wrong with this

for i in 71 72 73 74 75
do
    for server in server1 server2
    do
        somestr="Some String"
        echo "$server hdiskpower$i \c" ; ssh $server "lsattr -El hdiskpower$i |grep $somestr "
    done
done
A: 

You must use -t parameter to set ssh to use pseudo-tty. If you'd like to run that command on a remote machine, you'd do it by

ssh $server -t "lsattr -El hdiskpower$i|grep $somestr"
vtorhonen
Nope it still says grep: 0652-033 Cannot open volume.I am on AIX if that matters
Lisa
Since it's an error reported by grep, the problem is in the command you are sending. If you run that exact same command on the remote machine without the ssh, does it work? For example `lsattr -El hdiskpower71|grep "somethingsomething"`.
vtorhonen
yes it works without ssh and if hardcode the values instead of variables
Lisa
Interesting. What if you try it with some other command just to test grep, for example `ssh $server -t "echo \"test\"|grep test"`?
vtorhonen
ssh $server -t "echo \"test\"|grep test"This works and i get the result test
Lisa
Its strange that if i do ssh $server -t "lsattr -El hdiskpower$i|grep hardcodedstring "it works. So its definately gotta do something with variable expansion i guess
Lisa
Okay, so the problem is probably in your `$somestr`. Can you say that the variable contains? If it contains special characters such as `<, >, "` you must escape them with `\`.
vtorhonen
well it containts spaces but nothing else something like Physical Volume Identifier
Lisa
Yes, there's your problem. If you make a variable for the search string you have to escape it for whitespace also. So if your $string would be `string="Physical Volume Identifier"` you'd have to escape it as `string="Physical\ Volume\ Identifier"`. Another option is to escape the string before `grep`: `ssh $server -t "lsattr -El hdiskpower$i|grep \"$somestr\""`. Hope this helps.
vtorhonen
A: 

Hi vtorhonen

Your answer is correct thank you for all your help. Your coding skills were amazing

I lost my cookies and i am unable to mark your answer as correct.

Lisa