Skip to content

Commit c6336d6

Browse files
authored
v1.1
- Cleaned up loop logic (learned a bit more about the roles of break and continue) - Removed ~/.ssh dir check function. Added a loop in main section to check if ~/.ssh exists, if not, ask user to create it. If user says no, it returns to main loop (menu) instead of quitting the script. If user says yes, the create keys [3] section continues. - Fixed up some formatting errors made by TextWrangler (if handles tabs & spaces on the same line different for some reason)
1 parent b1f7a30 commit c6336d6

File tree

1 file changed

+83
-102
lines changed

1 file changed

+83
-102
lines changed

sshsetup.sh

Lines changed: 83 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CURRENTDIR=$(PWD)
77
############################
88

99
############################
10-
# Environment #
10+
# Environment #
1111
############################
1212

1313
## Colour text variables
@@ -46,16 +46,14 @@ menuprompt () {
4646
read -p "Main Menu? [y/n]: " ANS2
4747
if [ "$ANS2" = "n" -o "$ANS2" = "N" ];
4848
then
49-
continue
49+
:
5050
elif [ "$ANS2" = "y" -o "$ANS2" = "Y" ];
5151
then
5252
echo
5353
echo "Returning..."
5454
echo
5555
sleep 0.7
5656
break
57-
else
58-
continue
5957
fi
6058
done
6159
}
@@ -69,53 +67,15 @@ proceedprompt () {
6967
read -p "Proceed? [y/n]: " ANS4
7068
if [ "$ANS4" = "n" -o "$ANS4" = "N" ];
7169
then
72-
continue
70+
:
7371
elif [ "$ANS4" = "y" -o "$ANS4" = "Y" ];
7472
then
7573
sleep 0.7
7674
break
77-
else
78-
continue
7975
fi
8076
done
8177
}
8278

83-
## ~/.ssh check and create function
84-
85-
sshcheckandcreate () {
86-
SSHDIRVAR=n
87-
if [ ! -d $HOME/.ssh ];
88-
then
89-
read -p ""$C"$HOME/.ssh"$D" does not exist. This is where our ssh keys and config file should be stored. Create it now? [y/n]: " SSHDIRVAR
90-
if [ "$SSHDIRVAR" = "y" -o "$SSHDIRVAR" = "Y" ];
91-
then
92-
echo
93-
echo "Attempting to backup $HOME/.ssh as sshbak to your home folder just in case. ignore the error this will produce, as"\
94-
"it means $HOME/.ssh doesn't exist, and we're clear to create it."
95-
echo
96-
sleep 1
97-
cp -r $HOME/.ssh $HOME/sshbak
98-
echo
99-
sleep 1
100-
echo "Creating $HOME/.ssh..."
101-
echo
102-
sleep 1
103-
mkdir $HOME/.ssh
104-
echo "Created! Continuing..."
105-
echo
106-
sleep 0.5
107-
else
108-
echo
109-
echo ""$R"Not creating"$D". This tool requires ~/.ssh/ to exist."
110-
echo
111-
echo "Returning..."
112-
echo
113-
sleep 3
114-
continue
115-
fi
116-
fi
117-
}
118-
11979
## Remote host variables check function
12080

12181
remotevarcheck () {
@@ -162,7 +122,7 @@ remotevarcheck () {
162122
echo "Please run option 2 first."
163123
echo
164124
menuprompt
165-
continue
125+
break
166126
fi
167127
}
168128

@@ -203,8 +163,6 @@ do
203163
sudo apt-get install openssh-server
204164
echo
205165
menuprompt
206-
207-
continue
208166

209167
## 2. Configure remote host variables
210168

@@ -224,9 +182,7 @@ do
224182
if [ "$PORTVAR" = "" ] || [[ ! "$PORTVAR" =~ ^-?[0-9]+$ ]];
225183
then
226184
echo ""$R"Please enter a number"$D"."
227-
continue
228185
fi
229-
break
230186
done
231187
#IP
232188
while [[ $IPVAR = "" ]] || [[ ! $IPVAR =~ ^-?[0-9.]+$ ]];
@@ -235,9 +191,7 @@ do
235191
if [ "$IPVAR" = "" ] || [[ ! "$IPVAR" =~ ^-?[0-9.]+$ ]];
236192
then
237193
echo ""$R"Please enter an ip (x.x.x.x)"$D"."
238-
continue
239194
fi
240-
break
241195
done
242196
#Username
243197
while [[ $USERVAR = "" ]] || [[ $USERVAR = *[[:space:]]* ]];
@@ -246,9 +200,7 @@ do
246200
if [ "$USERVAR" = "" ] || [[ "$USERVAR" = *[[:space:]]* ]];
247201
then
248202
echo ""$R"Please enter a username without spaces"$D"."
249-
continue
250203
fi
251-
break
252204
done
253205
#Hostname
254206
while [[ $HOSTVAR = "" ]] || [[ $HOSTVAR = *[[:space:]]* ]];
@@ -257,18 +209,14 @@ do
257209
if [ "$HOSTVAR" = "" ] || [[ "$HOSTVAR" = *[[:space:]]* ]];
258210
then
259211
echo ""$R"Please enter a hostname without spaces"$D"."
260-
continue
261212
fi
262-
break
263213
done
264214
echo
265215
sleep 0.5
266216
echo ""$G"Configured!"$D""
267217
echo
268218
menuprompt
269-
270-
continue
271-
219+
272220
## 3. Setup & send ssh keys to remote host
273221

274222
elif [ $ANS = 3 ];
@@ -277,43 +225,82 @@ do
277225
clear
278226
border "Setup & Send SSH Keys"
279227
echo
280-
# First check to see if ~/.ssh exists. Create it if it doesn't. Function
281-
sshcheckandcreate
282-
cd $HOME/.ssh
283-
# Check to see if remote host variables are set
284-
remotevarcheck
285-
echo
286-
proceedprompt
287-
echo
288-
echo ""$G"Creating"$D" keys "$C"$HOSTVAR"$D" & "$C"$HOSTVAR.pub"$D" at ~/.ssh/. An empty passphrase stores keys in plain text."
289-
echo
290-
sleep 0.5
291-
ssh-keygen -f $HOSTVAR;
292-
echo
293-
sleep 0.5
294-
echo ""$G"Created!"$D""
295-
echo
296-
sleep 0.7
297-
echo "Attempting to send keys to "$C"$IPVAR"$D" on ssh port "$C"$PORTVAR"$D" with username "$C"$USERVAR"$D"."
298-
echo
299-
echo ""$P"Note"$D": If this is your first time logging in to "$C"$HOSTVAR"$D", you'll need to respond 'yes' to add its"\
300-
"fingerprint to your ~/.ssh/known_hosts file. You'll need to provide ssh with "$C"$USERVAR"$D"'s password on "$C"$IPVAR"$D"."
301-
echo
302-
sleep 0.7
303-
proceedprompt
304-
echo
305-
ssh-copy-id -i $HOSTVAR.pub -p $PORTVAR $USERVAR@$IPVAR
306-
sleep 0.7
307-
echo ""$G"Keys transferred!"$D""
308-
echo
309-
sleep 0.7
310-
echo ""$P"Note"$D": Passwordless login via rsa keys won't work until you run option 4 (add ~/.ssh/config entry)."
311-
echo
312-
sleep 0.7
313-
menuprompt
314-
315-
continue
316-
228+
# First check to see if ~/.ssh exists. Create and continue if user agrees
229+
SSHDIRVAR=y
230+
while [ "$SSHDIRVAR" = "y" -o "$SSHDIRVAR" = "Y" ];
231+
do
232+
if [ ! -d $HOME/.ssh ];
233+
then
234+
read -p ""$C"$HOME/.ssh"$D" "$R"does not exist"$D". This is where our ssh keys and config file should be stored. Create it now? [y/n]: " SSHDIRVAR
235+
236+
if [ "$SSHDIRVAR" = "y" -o "$SSHDIRVAR" = "Y" ];
237+
then
238+
echo
239+
echo "Attempting to backup $HOME/.ssh as sshbak to your home folder just in case. ignore the error this will produce, as"\
240+
"it means $HOME/.ssh doesn't exist, and we're clear to create it."
241+
echo
242+
sleep 1
243+
cp -r $HOME/.ssh $HOME/sshbak
244+
echo
245+
sleep 1
246+
echo ""$C"Creating"$D" $HOME/.ssh..."
247+
echo
248+
sleep 1
249+
mkdir $HOME/.ssh
250+
echo ""$G"Created"$D"! Continuing..."
251+
sleep 0.5
252+
253+
elif [ "$SSHDIRVAR" = "n" -o "$SSHDIRVAR" = "N" ];
254+
then
255+
echo
256+
echo ""$R"Not creating"$D". This tool requires ~/.ssh/ to exist."
257+
echo
258+
echo "Returning..."
259+
echo
260+
sleep 2
261+
else
262+
SSHDIRVAR=y
263+
264+
fi
265+
elif [ -d $HOME/.ssh ];
266+
then
267+
# Continue with key creation
268+
cd $HOME/.ssh
269+
# Check to see if remote host variables are set
270+
remotevarcheck
271+
echo
272+
proceedprompt
273+
echo
274+
echo ""$G"Creating"$D" keys "$C"$HOSTVAR"$D" & "$C"$HOSTVAR.pub"$D" at ~/.ssh/. An empty passphrase stores keys in plain text."
275+
echo
276+
sleep 0.5
277+
ssh-keygen -f $HOSTVAR;
278+
echo
279+
sleep 0.5
280+
echo ""$G"Created!"$D""
281+
echo
282+
sleep 0.7
283+
echo "Attempting to send keys to "$C"$IPVAR"$D" on ssh port "$C"$PORTVAR"$D" with username "$C"$USERVAR"$D"."
284+
echo
285+
echo ""$P"Note"$D": If this is your first time logging in to "$C"$HOSTVAR"$D", you'll need to respond 'yes' to add its"\
286+
"fingerprint to your ~/.ssh/known_hosts file. You'll need to provide ssh with "$C"$USERVAR"$D"'s password on "$C"$IPVAR"$D"."
287+
echo
288+
sleep 0.7
289+
proceedprompt
290+
echo
291+
ssh-copy-id -i $HOSTVAR.pub -p $PORTVAR $USERVAR@$IPVAR
292+
sleep 0.7
293+
echo ""$G"Keys transferred!"$D""
294+
echo
295+
sleep 0.7
296+
echo ""$P"Note"$D": Passwordless login via rsa keys won't work until you run option 4 (add ~/.ssh/config entry)."
297+
echo
298+
sleep 0.7
299+
menuprompt
300+
break
301+
fi
302+
done
303+
317304
## 4. Add remote host entry to ~/.ssh/config
318305

319306
elif [ $ANS = 4 ];
@@ -359,9 +346,7 @@ do
359346
echo
360347
sleep 0.7
361348
menuprompt
362-
363-
continue
364-
349+
365350
## 5. Add remote host entry to /etc/hosts
366351

367352
elif [ $ANS = 5 ];
@@ -408,8 +393,6 @@ do
408393
echo
409394
sleep 0.7
410395
menuprompt
411-
412-
continue
413396

414397
## 6. Quitting
415398

@@ -424,8 +407,6 @@ do
424407
echo
425408
echo "$R"Invalid selection. Returning..."$D"
426409
echo
427-
sleep 0.7
428-
continue
410+
sleep 0.7
429411
fi
430-
break
431412
done

0 commit comments

Comments
 (0)