vendredi 21 avril 2017

Nested If Loop with ssh

I am attempting to create a script that will A) cycle through a list of IP's and ping them, if successful attempt to log in using known credentials and if successful with that add the host to a list so the actions do not repeat.

This script runs as expected if A) the host is unavailable to ping or B) if it is available and my known credentials work. If it can ping the host but the ssh connection is rejected the script quits instead of using the next variable (or IP). Thanks!

What I have so far:

while read p; do
ping -c1 -t1 $p
if [ $? -eq 0 ]
  then
    /usr/local/bin/sshpass -p $PW ssh -o StrictHostKeyChecking=no "$USR"@"$p" </dev/null "
    scutil --get ComputerName" | tee -ai $WORKING

    MACHINE=$(head -n 1 $WORKING)

    if grep -Fxq $MACHINE "$FULL"
      then
        echo $MACHINE has been worked on already

      else
        echo $MACHINE has not been worked on
        echo $MACHINE >> $FULL
        rm -rf $WORKING
      fi

else
echo ping fail
fi

done

Aucun commentaire:

Enregistrer un commentaire