Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 780
zaslayer Avatar asked Sep 14 '26 00:09

zaslayer


2 Answers

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.

like image 113
Mort Avatar answered Sep 16 '26 14:09

Mort


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"
like image 26
Slava Semushin Avatar answered Sep 16 '26 13:09

Slava Semushin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!