There are only 2 return values that I expect when running ssh -T git@github.com
:
1
: user is authenticated, but cannot open a shell with GitHub
255
: user is not authenticated
You will never get a return code of 0 for exactly the reason @VonC described. That means you can't use fun bash shorthands for checking return codes, like short-circuiting logic checks - you must be explicit in recording and checking $?
.
Here's a shell script I use to check if I'm auth'd to GitHub:
function github-authenticated() {
# Attempt to ssh to GitHub
ssh -T git@github.com &>/dev/null
RET=$?
if [ $RET == 1 ]; then
# user is authenticated, but fails to open a shell with GitHub
return 0
elif [ $RET == 255 ]; then
# user is not authenticated
return 1
else
echo "unknown exit code in attempt to ssh into git@github.com"
fi
return 2
}
You can use it casually from the command line like so:
github-authenticated && echo good
or more formally in a script like:
if github-authenticated; then
echo "good"
else
echo "bad"
fi