1

Why when using sshpass in a loop the first sshpass executes commands, while the next does not?

for element in "${a[@]}"
 do {
     echo "$element"
     sshpass -p "omg" ssh root@$element 'ls' 
    }
done
5
  • Can we have some context, like for instance what a is? Jan 11, 2019 at 18:26
  • a is an array with containing ip's of VM's
    – Goking
    Jan 11, 2019 at 18:32
  • fixed it use: echo "$element"; export SSHPASS='omg' ;sshpass -e ssh -o StrictHostKeyChecking=no root@$element 'ls' unset SSHPASS
    – Goking
    Jan 11, 2019 at 18:42
  • Do not add additional information in the comment section. Better edit your question with the new information.
    – zx485
    Jan 11, 2019 at 18:44
  • @Goking make an answer to this question and mark it as the solution. Remove the fixed code sample from the question and place it in the solution. Jan 11, 2019 at 19:32

1 Answer 1

0

I found a fix:

for element in "${a[@]}" do
   echo "$element"
   export SSHPASS='omg'
   sshpass -e ssh -o StrictHostKeyChecking=no root@$element 'ls'
   unset SSHPASS
done  
2
  • 1
    Note: the question is "why?" and the answer explains nothing. It may or may not be an example of voodoo programming, but without explanation it will be useful mostly to voodoo programmers. Jan 21, 2019 at 19:23
  • I guess it has to do something with the prompt : add this host? and you have to type yes . Besides that it works perfectly fine without the loop, even the host is not added in a script it assumes yes.
    – Goking
    Jan 23, 2019 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .