This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
ssh-test
executable file
·77 lines (59 loc) · 1.43 KB
/
ssh-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#
# Functions
#
function usage_and_exit {
cat << EOF >&2
Usage: $0 <host-file>
Show whether it is possible to ssh into hosts listed in <host-file>.
Hostnames in <host-file> are separated by newlines and have the format:
[user@]hostname[:port]
Sample <host-file>:
example.vm.host.tld
[email protected]:2222
[email protected]:2224
EOF
exit 1
}
#
# Command line options and arguments parsing
#
while getopts :h opt; do
case $opt in
h) usage_and_exit
;;
'?') echo "$0 Invalid option -$OPTARG" >&2
usage_and_exit
;;
esac
done
shift $((OPTIND - 1)) # remove options, leave arguments
if [ $# -ne 1 ]; then
usage_and_exit
fi
#
# MAIN
#
# Calculate the longest hostname, to align columns if we can.
longest_hostname=0
for line in $(cat "$1"); do
length=${#line}
[ $length -gt $longest_hostname ] && longest_hostname=$length
done
RED='\033[1;31m'
NC='\033[0m' # No Color
for line in $(cat "$1"); do
host=${line%:*}
port=${line#*:}
if [ ! -z "${port##*[!0-9]*}" ]; then
portarg="-p $port"
fi
printf "%${longest_hostname}s ... " $line
if ssh -q -o BatchMode=yes -o ConnectTimeout=3 $portarg $host exit 2>/dev/null; then
echo "OK"
else
printf "${RED}FAIL${NC}\n"
fi
done