So I have a function that looks like this. It's an oversimplified example but its enough to demonstrate my issue.
For some reason, I can not get the exit status of the ssh remote command. It does not matter if I give exit 0, or true or if I give a command that throws a success command, the end result is the same, I always get 1.
The thing is, this code works perfectly when ran from a script, but not if I do it from a function.
function hello(){
while read user password
do
ssh -n -q user@host 'exit 0' ; echo $?
done <<< "$( cat creds.txt )"
}
Remember that ssh doesn't allow to pass the password as an argument. You will need to use an external utility like sshpass or public/private key.
With sshpass, the function will look like this:
I tried the script with a list of 2 hosts; one with a good password and one with a bad password:
It looks that you want to do automation for Linux machines. It is time to start learning ansible or any other configuration management.
Please read also about XY problem.