This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SetupSSH
executable file
·227 lines (186 loc) · 4.62 KB
/
SetupSSH
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/sh
# Gaomingjie - 20180527
# -- envs --
TEMP=/tmp
LOGFILE=$TEMP/SetupSSH_`date +%F-%H-%M-%S`.log
# -- global --
nargs=$#
n=1
logfile=$LOGFILE
platform=`uname -s`
confirm=no
# -- function --
usage()
{
cat <<EOF
$0
Description:
Usage:
sshUserSetup.sh
-user <user name>
-hosts \"<space separated hostlist>\" | -hostfile <absolute path of cluster configuration file>
Examples:
EOF
}
verify_bins()
{
SSH="/usr/bin/ssh"
SCP="/usr/bin/scp"
SSH_KEYGEN="/usr/bin/ssh-keygen"
PING="/bin/ping"
PATH_ERROR=0
if test ! -x $SSH ; then
echo "ssh not found at $SSH. Please set the variable SSH_PATH to the correct location of ssh and retry."
PATH_ERROR=1
fi
if test ! -x $SCP ; then
echo "scp not found at $SCP. Please set the variable SCP_PATH to the correct location of scp and retry."
PATH_ERROR=1
fi
if test ! -x $SSH_KEYGEN ; then
echo "ssh-keygen not found at $SSH_KEYGEN. Please set the variable SSH_KEYGEN_PATH to the correct location of ssh-keygen and retry."
PATH_ERROR=1
fi
if test ! -x $PING ; then
echo "ping not found at $PING. Please set the variable PING_PATH to the correct location of ping and retry."
PATH_ERROR=1
fi
if [ $PATH_ERROR = 1 ]; then
echo "ERROR: one or more of the required binaries not found, exiting"
exit 1
fi
}
verify_params()
{
echo -- Verify Params --
if test -z "$hosts"; then
if test -n "$host_file" && test -f "$host_file"; then
hosts=`awk '$1 !~ /^#/ { str = str " " $1 } END { print str }' $host_file`
elif ! test -f "$host_file"; then
echo "Please specify a valid and existing hosts file."
fi
fi
if test -z "$hosts" || test -z $user
then
echo "Either user name or host information is missing"
usage
exit 1
fi
if [ -d $logfile ]; then
echo $logfile is a directory, setting logfile to $logfile/ssh.log
logfile=$logfile/ssh.log
fi
echo The output of this script is also logged into $logfile | tee -a $logfile
if [ `echo $?` != 0 ]; then
echo Error writing to the logfile $logfile, Exiting
exit 1
fi
echo Hosts are $hosts | tee -a $logfile
echo User is $user | tee -a $logfile
}
verify_os()
{
echo | tee -a $logfile
echo -- Verify OS -- | tee -a $LOGFILE
case "$platform" in
"Linux") os=linux;;
*) echo "Sorry, $platform is not currently supported." | tee -a $LOGFILE
exit 1;;
esac
echo "Platform: $platform " | tee -a $LOGFILE
}
verify_hosts()
{
echo | tee -a $logfile
echo -- Verify Hosts -- | tee -a $LOGFILE
for host in $hosts; do
$PING -c 5 -w 5 $host > /dev/null
echo Ping $host done.
exitcode=`echo $?`
if [ $exitcode = 0 ]; then
alivehosts="$alivehosts $host"
else
deadhosts="$deadhosts $host"
fi
done
if test -z "$deadhosts"
then
echo Remote host reachability check succeeded. | tee -a $LOGFILE
echo The following hosts are reachable: $alivehosts. | tee -a $LOGFILE
echo The following hosts are not reachable: $deadhosts. | tee -a $LOGFILE
echo All hosts are reachable. Proceeding further... | tee -a $LOGFILE
else
echo Remote host reachability check failed. | tee -a $LOGFILE
echo The following hosts are reachable: $alivehosts. | tee -a $LOGFILE
echo The following hosts are not reachable: $deadhosts. | tee -a $LOGFILE
echo Please ensure that all the hosts are up and re-run the script. | tee -a $LOGFILE
echo Exiting now... | tee -a $LOGFILE
exit 1
fi
numhosts=`echo $hosts | awk '{ }; END {print NF}'`
echo Hosts number: $numhosts
}
# -- entry --
while [ $n -le $nargs ]; do
par=$1
if [ $par = "-help" ]; then
usage
exit
fi
if [ $par = "-hosts" ]
then
hosts=$2
shift 1
n=`expr $n + 1`
fi
if [ $par = "-hostfile" ]
then
host_file=$2
shift 1
n=`expr $n + 1`
fi
if [ $par = "-user" ]
then
user=$2
shift 1
n=`expr $n + 1`
fi
if [ $par = "-logfile" ]
then
logfile=$2
shift 1
n=`expr $n + 1`
fi
if [ $par = "-confirm" ]
then
confirm=yes
fi
# shift if no match
shift 1
n=`expr $n + 1`
done
# -- verify --
verify_params
verify_os
verify_bins
verify_hosts
# -- setup ssh --
echo SetupSSH will setup SSH connectivity from the local host to all the remote hosts. | tee -a $logfile
echo | tee -a $logfile
echo -- User Confirm -- | tee -a $logfile
echo "Do you want to continue (yes/no)?" | tee -a $logfile
if [ "$confirm" = "no" ]
then
read confirm
else
echo "Confirmation provided on the command line." | tee -a $logfile
fi
echo | tee -a $logfile
echo The user choose ''$confirm'' | tee -a $logfile
echo | tee -a $logfile
echo -- Setup SSH -- | tee -a $logfile
if [ "$confirm" = "no" ]; then
echo "SSH setup is not done." | tee -a $logfile
exit 1
else
exit