/
sshMenu.sh
180 lines (158 loc) · 5.07 KB
/
sshMenu.sh
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
#!/usr/bin/bash
QUIT=0
#If your userid is not a local variable add it here
#or use the menu option to change the value
#USERID=
SERVERARRAYDEV=(devapp01 devapp02 devdb01 devdb02)
SERVERARRAYQA=(qaapp01 qaapp02 qadb01 qadb02)
SERVERARRAYPROD=(prdapp01 prdapp02 prddb01 prddb02)
#####################################################
# DISPLAY MENU FOR SSH OPTIONS #
#####################################################
display_menu(){
echo
echo "###########################################"
echo "# #"
echo "# Set up keys for remote servers #"
echo "# for user ${USERID} #"
echo "# #"
echo "###########################################"
echo "C. Press U to create your local RSA key"
echo "D. Press D to load your rsa key to all remote development servers"
echo "Q. Press Q to load your rsa key to all remote qa servers"
echo "P. Press P to load your rsa key to all remote production servers"
echo "A. Press A to load your rsa key to all remote servers"
echo "S. Press S to load your rsa key to a specific remote servers"
echo "U. Press U to enter a different user id."
echo "Q. Press Q to quit."
}
#####################################################
# THIS TAKES THE INPUT PROVIDED BY THE USER #
# AND EXECUTED THE APPROPRIATE OPTION USING CASE #
#####################################################
menu_options(){
display_menu
echo "Please select option from the list above"
read OPTION
case $OPTION in
[cC]) echo "Generating Local RSA key"
generateLocalKey
;;
[dD]) echo "Loading key for development servers"
SERVERARRAY="${SERVERARRAYDEV[@]}"
runserverlist
;;
[qQ]) echo "Loading key for quality assurance servers"
SERVERARRAY=("${SERVERARRAYQA[@]}"
runserverlist
;;
[pP]) echo "Loading key for production servers"
SERVERARRAY="${SERVERARRAYPROD[@]}"
runserverlist
;;
[aA]) echo "Loading key for all servers"
SERVERARRAY=( ${SERVERARRAYDEV[@]]} ${SERVERARRAYQA[@]} ${SERVERARRAYPROD[@]]} )
runserverlist
;;
[sS]) echo "Enter the server name to load the rsa key to:"
read SERVERARRAY
echo "you entered ${SERVER}"
runserverlist
;;
[uU]) echo "Enter a new user id:"
read USERID
echo "you entered ${USERID}"
;;
[qQ]) QUIT=1;;
*) echo "Invalid selection. Please try again"
;;
esac
}
#####################################################
# GENERATE LOCAL RSA KEY IF IT DOESNT EXIST #
#####################################################
generateLocalKey(){
if [ -f ~/.ssh/id_rsa.pub ]
then
echo ".ssh/id_rsa.pub already exists"
else
echo ".ssh/id_rsa.pub does not exists ... running ssh-keygen"
echo "Press enter for all three inputs"
ssh-keygen -t rsa;
chmod 600 .ssh/id_rsa
fi
}
#####################################################
# THIS FUNCTION RUNS THE loadscp FUNCTION #
# FOR THE VALUE IN $SERVER #
#####################################################
loadscp(){
if [ -f .ssh/id_rsa.pub ]
then
echo "uploading .ssh/id_rsa.pub for <${USERID}> to server <${SERVER}>"
else
generateLocalKey
fi
echo "checking for file .ssh/authorized_keys for user <${USERID}> on server <${SERVER}>"
if ssh $USERID@$SERVER test -e .ssh/authorized_keys
then
echo "file .ssh/authorized_keys already exists"
else
echo "RUNNING loadscp function for user <${USERID}> on server <${SERVER}>"
scp .ssh/id_dsa.pub $USERID@$SERVER:
sshkeysetup
fi
}
#####################################################
# THIS FUNCTION COPIES THE id_dsa.pub FILE TO #
# .ssh/authorized_keys FOR THE VALUE IN $SERVER #
#####################################################
sshkeysetup(){
ssh $USERID@$SERVER 'bash -s' <<EOF
if [ -d .ssh ]
then
echo "Directory found"
else
echo "Directory not found ... creating directory .ssh"
mkdir .ssh
chmod 700 .ssh
fi
ls -la
if [ -f .ssh/authorized_keys ]; then
echo "file exists"
else
echo "authorized_keys file does not exist ... creating file"
cat id_dsa.pub >> .ssh/authorized_keys
fi
ls -al .ssh
sleep 5
chmod 700 .ssh
rm id_dsa.pub
pwd
sleep 5
ls -al
ls -al .ssh
sleep 5
exit
EOF
}
#####################################################
# THIS FUNCTION RUNS THE loadscp FUNCTION #
# FOR EACH VALUE IN THE ARRAY $SERVERARRAY #
#####################################################
runserverlist(){
for SERVER in ${SERVERARRAY[*]}
do
echo "RUNNING loadscp for <${USERID}> to server <${SERVER}>"
#removed function to take advantage of the newer ssh-copy-id command
#loadscp
ssh-copy-id $USERID@$SERVER
done
}
#####################################################
# DISPLAY MENU OPTIONS UNTIL Q IS SELECTED #
#####################################################
while [ $QUIT -lt 1 ]
do
menu_options
done