0

I'm writting a bash script to change all fortigate's passwords. I have to pick all IP public fortigates and I have to test if the connection is good or bad. With the command below I know that my ssh connection return some errors, 0=connection is good.

sshpass -p [PASSWORD] ssh -q -oStrictHostKeyChecking=no [USERNAME]@[LOCAL_IP] > /dev/null

Now I want to put this in my script to test all fortigates ssh connections and to put this in a mail. But when I put the command in my while loop, the loop is running only one time ( totaly work without testing ssh connection)

if [ "$?" == "0" ]
then
              echo "(./forti.sh) | sshpass -p $p ssh -oStrictHostKeyChecking=no $u@$IP" >> $base/change_password.sh           
                        ./change_password.sh
                        echo "<td id="rouge"></td>" >> pass_mail.html
                        rm $base/change_password.sh
else  
                        echo "<td id="rouge"></td>" >> pass_mail.html
                        rm $base/change_password.sh
fi

So this is my script block who verify the condition in case the fortigate is up, as I said before the while loop is running one time even if it's true.

Have you got any idea of my issue ?

Do not hesitate to ask details if it's not clear enough.

Thanks.

  • Always add double quotes around your variable expansion and protect the options of the `rm` command. This is very risky and expose to irreversible data loss: `rm $base/change_password.sh`. Write instead: `rm -f -- "$base/change_password.sh"`. `--` terminates the options arguments and prevents injection of rogue options from the variables used in following arguments. – Léa Gris Aug 12 '20 at 15:25
  • Thanks a lot for yours suggestions i'll do this. – Maxence Touzard Aug 12 '20 at 15:31

0 Answers0