How do I write the exitcode of a remote sshpass command into a variable in my local script for further use?
I am using sshpass in order to remotely execute a command. I know that certificate based authentication is better, but it's not my system and I can not set up cert based authentication.
My problem is that I am not able to catch the exit code of the remote command into my variable $errm.
My command looks like this:
#!/bin/bash
sshpass -p $pass ssh -o StrictHostkeyChecking=no -t $user@$Hostip 'touch /does/not/exist.txt &>/dev/null errm=$?'
echo $errm
echo $errm does not show anything no matter if I give it a true path or one which /does/not/exist.txt. However, the file is created remotely if I provide a valid path, so everything sshpass related works but I just can't catch the exit code.
However, if I do it this way at least my script shows me the exit code of the remote command as it is running, but as I mentioned, I am not able to catch it in a variable for further use in the same script
sshpass -p $pass ssh -o StrictHostkeyChecking=no -t $user@$Hostip 'touch /does/not/exist.txt &>/dev/null && echo $? || echo $?'
I think the problem has something to do with the error code being thrown on the remote system while my script runs locally!?