[solved] Process ssh command in while loop

I have a script that reads a file containing a list of server names. It's suppose to loop through the list of names and execute a command on the remote server using ssh. It processes the ssh command for the first server in the list and then exits. Here's the code:

#!/bin/bash

FILENAME=server_list.txt

cat $FILENAME | while read LINE
do
        echo "$LINE"

        # Run the Users.sh script on the remote server
        ssh root@$LINE '/usr/local/bin/Users.sh > /root/UserAccounts.txt'

done

When I comment out the ssh command the script processes as expected. Does ssh do something when it exits the first time that is maybe stopping the rest of the script from processing? I'd appreciate any suggestions.

---------- Post updated at 05:20 PM ---------- Previous update was at 04:28 PM ----------

I found the answer. Gotta use the -n option in the ssh command

ssh -n root@$LINE '/usr/local/bin/Users.sh > /root/UserAccounts.txt'
1 Like

Thanks for posting the solution.

while read LINE <&3
do
 echo "$LINE"
 ssh -nx root@"$LINE" '...'
done 3< $FILENAME

This not only avoids the "useless use of cat": the additional <&3 and 3 < allow to even have the bare ssh command. The -n and -x are still there for efficiency reason.