Lisa
Lisa

Reputation: 13

Sending commands over ssh with parameters

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

Upvotes: 1

Views: 6938

Answers (3)

MLSC
MLSC

Reputation: 5972

I think this is what you need: At first you need to install sshpass on your machine. then you can write your own script:

while read pass port user ip; do
sshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1
    COMMAND 1
    .
    .
    .
    COMMAND n
ENDSSH1
done <<____HERE
    PASS    PORT    USER    IP
      .      .       .       .
      .      .       .       .
      .      .       .       .
    PASS    PORT    USER    IP    
____HERE

Upvotes: 0

mayank051285
mayank051285

Reputation: 89

If you are passing some variable to remote server inside a command, then those variable must be defined at that remote server.

Like, if you hardcoded the command in script like "echo "$server hdiskpower$i \c" ; ssh $server lsattr -El hdiskpower71 |grep XYZ" then it surely works for you.

Meanwhile i will work to make it workable with variables.

Upvotes: 1

thevilledev
thevilledev

Reputation: 2377

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"

Upvotes: 0

Related Questions