0

What is causing this script to exit the loop at line "program is exiting loop here"? In the context it will loop twice but it won't if i have the CM_CHECK_STATUS where i have it.

RUNLOG="log"
OUTPUTFILE="output"

LIST=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al" 2>> $RUNLOG > $OUTPUTFILE)

PS_VALS=$(cat $OUTPUTFILE)

if [[ $? != 0 ]] then echo "There was an error at:" date >> $RUNLOG else while IFS= read -r line; do # Check ls echo "... $line ..." CM_CHECK_STATUS=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al") #program is exiting loop here done <<< "$PS_VALS" echo "finished" fi

If i remove CM_CHECK_STATUS=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al") and place it anywhere else outside the loop that command works fine and the loop runs as expected. Is there something wrong with using that command inside a loop?

jtlindsey
  • 333
  • 1
    Use set -x to generate debug output. A few comments that are not directly related to your question: When you check $?, you check the success of the cat command. I wonder if that is your intention. Why do you use <<< instead of just redirecting input from $OUTPUTFILE. And processing text files with while... read can be a bad idea, one being that something in the loop body may consume the input, leading to a premature EOF. Which may well be the case here, but right now my mind is unable to cut through the sshpass commands. – berndbausch Apr 18 '21 at 04:14
  • Also, $LIST is always going to be empty (stdout is redirected to $OUTPUTFILE). and isn't used anywhere in the script anyway. – cas Apr 18 '21 at 04:22
  • The most serious problem, though, is using sshpass instead of using ssh as it is meant to be used, with public key authentication. sshpass is an ugly hack with serious security implications that should only be used when nothing else works (or to automate ssh-copy-id when first setting up a bunch of machines, or adding a new machine), not just to avoid the trivial effort of setting up public key auth. – cas Apr 18 '21 at 04:28
  • Finally, WTF is the actual purpose of the script? what is it meant to achieve? It looks like it's intended to get a directory listing from a remote host and then, once for every line in that list, go fetch the same directory list again. Nothing else is done with the contents of any of the directory listings. Looks like an XY Problem to me. – cas Apr 18 '21 at 04:35
  • @berndbausch Yes i am intending on checkout output of cat command with $?. The script has been reduced from its original (much larger/longer doing other things) to get the minimum to reproduce the problem. @cas $LIST is used later in the larger script. The only reason i'm using the sshpass instead of ssh with key is i'm connecting to a embedded ESXi system running on a flashdrive in a dell server. The default setup doesn't allow me to easily persist ssh public key or otherwise I would use regular ssh. sshpass -f lets me secure pw in file on machine running this script in a cron job. – jtlindsey Apr 18 '21 at 05:02
  • The main issue is ssh inheriting standard input from the loop and reading all that is left to read. The solution to this is to redirect /dev/null into ssh, ro to start ssh with its -N option (which has the same effect). There are several other issue with your script that you may want to ask separate questions about, or brows question and answers about on this site. For example, how to feed the output of a command into a loop (without storing the result in an intermediate variable), and what command substitutions are usually used for (not to get the "status" from a command). – Kusalananda Apr 18 '21 at 09:49
  • Not sure who found the question "SSH causes while loop to stop" but thank you. I had no idea ssh was causing the problem. set -x just confirmed it was in fact exiting the loop where the ssh command was but i didn't consider it being the problem because that same line works in other places. adding </dev/null to end of the command fixed the problem. – jtlindsey Apr 18 '21 at 12:03

0 Answers0