zaslayer zaslayer - 8 months ago 46
Bash Question

bypass the exit code in bash

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.