Created
June 12, 2017 20:28
-
-
Save rye/b2c6d8b88102d517d4aa26cd21e0dbfd to your computer and use it in GitHub Desktop.
Checks the status of a list of machines using SSH blank firing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function check_user_and_host { | |
_ssh_result=$(ssh -o ConnectTimeout=5 -o BatchMode=yes -o StrictHostKeyChecking=no $1@$2 "echo \"\`who | grep -c . \`\"; true" 2>/dev/null) | |
} | |
function check_and_output_host { | |
check_user_and_host $1 $2 && echo "$2: up, $_ssh_result users logged in" || echo "$2: down" | |
} | |
function store_worker_pid { | |
_workers[${#_workers[@]}]=$1 | |
} | |
function kill_all_workers () { | |
for pid in ${_workers[*]}; do | |
kill $pid >/dev/null 2>&1 | |
done | |
} | |
function usage { | |
cat <<EOF | |
Usage: $0 <user> <host> [<host> ...] | |
Tests SSH logins by <user> to all <hosts> in parallel, printing | |
reports as information becomes available and waiting until all | |
commands exit. Keeps a list of PID's and kills all of them if SIGINT, | |
SIGQUIT, SIGTSTP are signalled. | |
If <host> is up and allows passwordless authentication for <user>, | |
(highly recommended as otherwise password prompts will be given) runs | |
\`who | grep -c .\` and prints output before passing. Useful for | |
gathering user counts on all servers and checking status at a high | |
level. | |
You can (and are encouraged) to use brace expansion in your argument | |
list, to simplify generating a list of hosts. There is theoretically | |
no maximum imposed by this script, instead just by the maximum number | |
of file descriptors open on your kernel. | |
EOF | |
} | |
case $1 in | |
--help|-h) | |
usage | |
exit | |
;; | |
*) | |
trap kill_all_workers SIGINT SIGQUIT SIGTSTP | |
for i in ${@:2}; do | |
check_and_output_host $1 $i & | |
store_worker_pid $! | |
done | |
for pid in ${_workers[*]}; do | |
wait $pid >/dev/null 2>&1 | |
done | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment