Skip to content

Instantly share code, notes, and snippets.

@mikesimons
Created April 7, 2016 10:53
Show Gist options
  • Save mikesimons/a24181bcfd48e56479ce6c417e8b6441 to your computer and use it in GitHub Desktop.
Save mikesimons/a24181bcfd48e56479ce6c417e8b6441 to your computer and use it in GitHub Desktop.
Script for testing access to a single ssh host.Loop with something like: cat ~/my-hosts | xargs -I X bash -c 'echo -n "X: " && ssh-test.sh myusername@X'
#!/bin/bash
main() {
declare host="$1"
ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -oBatchMode=yes -oConnectTimeout=5 -vvv $host -- exit > /tmp/sshtest 2>&1
if [[ $? == 0 ]]; then
echo "OK"
exit
fi
if cat /tmp/sshtest | grep "^ssh:.*Connection refused" > /dev/null; then
echo "CONNECTION REFUSED"
exit 1
fi
if cat /tmp/sshtest | grep "^Permission denied" > /dev/null; then
echo "PERMISSION DENIED"
exit 2
fi
if cat /tmp/sshtest | grep "^ssh:.*Connection timed out" > /dev/null; then
echo "TIMEOUT"
exit 3
fi
echo "UNKNOWN"
exit 4
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment