So I have a little script that is supposed to SSH into a 1000 or so accounts over different servers, as below.
for account in $(cat $SSH_LIST)
do echo -e "\n$account\n"
SERVER=$(echo $account | cut -d',' -f1 | awk '{print tolower($0)}') ; USER=$(echo $account | cut -d',' -f2) ; PASS=$(echo $account | cut -d',' -f3)
sshpass -p $PASS ssh -o StrictHostKeyChecking=no $USER@$SERVER "ls passwd.cdb && exit"
Problem is, the moment that ssh hits a permission denied, the script exits. Understandably, the SSH Permission Denied acts like an exit code and exits out the script. I need it to simply log the denied permission and continue on its merry way SSH'ing.
Any advice?
Normally, bash would not exit on the first error, unless set -e is set higher up in your script. Try putting this before your for loop.
set +e
p.s. You're missing a done at the end of your loop. I assume that's just a cut and paste error.
Try the following:
if ! sshpass -p $PASS ssh -o StrictHostKeyChecking=no $USER@$SERVER "ls passwd.cdb && exit"; then
echo >&2 "WARNING: access denied for $USER@$SERVER"
fi
Also you can use logical operators OR:
sshpass -p $PASS ssh -o StrictHostKeyChecking=no $USER@$SERVER "ls passwd.cdb && exit" ||
echo >&2 "WARNING: access denied for $USER@$SERVER"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With