I'm trying to write a bash script that will run through all the .rdp files in a folder and try to connect with each of them in turn using FreeRDP, but once it successfully connects to one, I need it to stop. What's the best way to test whether FreeRDP successfully connected to a remote desktop?
This was my first thought:
for file in filepath/*.rdp;
do
echo "attempting connection to $file"
if xfreerdp "$file" /u:"$username" /p:"$password";
then
echo "Connection to $file succeeded."
break
else
echo "Connection to $file failed."
fi
done
but this will basically never trigger the break, because when you disconnect from the remote desktop by logging out, etc, FreeRDP will return a non-zero exit code.
I found a list of the FreeRDP exit codes here, but I'm not sure it's comprehensive. Logging out of the desktop, for example, returns exit code 12, which isn't on the list.
But even if I understood what all the possible codes are and what they're for, I'd still have to just arbitrarily decide which ones to consider success and which ones to consider failure, which doesn't seem like a great solution.
The simplest option I can think of is to use if [[ $? -le 15 ]]
, which I'm pretty sure would work 99% of the time for my purposes, but clearly isn't robust.
Is there a cleaner way to do this? If there's a way to do the test at the time the connection is made instead of waiting for the program to exit, that would be even better, but I'm not sure that's possible since the shell runs one command at a time.