Skip to content

Commit 8e6a5fd

Browse files
committed
added ssh options (e.g to build tunnels )
2 new functions : addNewServerOption() editServerOption() menu ordering change thanks to Rouzic6666
1 parent 3dbe13e commit 8e6a5fd

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

ssh-manager.sh

+46-27
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fileEmptyCheck(){
2727
fi
2828
}
2929

30-
addNewServer(){
30+
addNewServerName(){
3131

3232

3333
read -p "Enter the server name/alias: " name
@@ -36,13 +36,13 @@ read -p "Enter the server name/alias: " name
3636
if [ -z "$name" ] # -z checks if the string is empty
3737
then
3838
printf "%s${warning}Server name cannot be blank${reset}\n"
39-
addNewServer
39+
addNewServerName
4040
fi
4141

4242

4343
}
4444

45-
editServer(){
45+
editServerName(){
4646

4747
printf "Current name is: %s \n " "$name"
4848
read -p "Enter the new name or leave blank to use current name : " newName
@@ -150,7 +150,7 @@ fi
150150

151151
}
152152

153-
editServerUser(){
153+
editServerUser(){
154154

155155
printf "Current user is %s \n " "$user"
156156
read -p "Enter the new username or leave blank to use current name : " newUser
@@ -180,7 +180,7 @@ then
180180
fi
181181
}
182182

183-
editServerKeyFile(){
183+
editServerKeyFile(){
184184

185185
printf "Current key file is %s \n " "$keyfile"
186186
read -p "Enter the path to the key file or leave blank to use current key file: " newKeyFile
@@ -201,21 +201,33 @@ fi
201201

202202
}
203203

204+
addNewServerOption(){
205+
read -p "Enter the options leave blank if not used: " option
206+
207+
}
208+
209+
editServerOption(){
210+
211+
printf "Current option is %s \n " "$option"
212+
read -p "Enter the path to the key file or leave blank to use current option: " newOption
213+
}
204214

205-
createNewSSHCredentials(){
215+
216+
createNewSSHCredentials(){
206217

207218
printf "%s${info}===========================${reset}\n"
208219
echo -e "${info}Add New SSH Connection${reset}"
209220
printf "%s${info}===========================${reset}\n"
210221

211-
addNewServer
222+
addNewServerName
212223
addNewServerIp
213224
addNewServerPort
214225
addNewServerUser
215226
addNewServerKeyFile
227+
addNewServerOption
216228

217229

218-
echo "$name,$ip,$port,$user,$keyfile" >> "$cfg_file_name"
230+
echo "$name,$ip,$port,$user,$keyfile,$option," >> "$cfg_file_name"
219231

220232
echo -e "${success}SSH Connection added successfully${reset}"
221233
read -p "Do you want to connect to the added SSH connection now? (y/n) " selection
@@ -236,7 +248,7 @@ fi
236248
printf "%s${info}===========================${reset}\n"
237249
#Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
238250
printf "%s${info}# Name IP/Host \tPort Username\tKey File${reset}\n"
239-
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works
251+
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works
240252

241253
printf "%s${warning}Enter the number of the SSH connection you want to edit or enter 0 to cancel : ${reset}"
242254
read -p "" serverNumber
@@ -260,16 +272,18 @@ fi
260272
port=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
261273
user=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $4}' "$cfg_file_name")
262274
keyfile=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $5}' "$cfg_file_name")
275+
option=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $6}' "$cfg_file_name")
263276

264-
editServer
277+
editServerName
265278
editServerIp
266279
editServerPort
267280
editServerUser
268281
editServerKeyFile
282+
editServerOption
269283

270284
#Now to replace the selected lines info with the updated info to the file
271285

272-
sed -i "${serverNumber}s/.*/$name,$ip,$port,$user,$keyfile/" "$cfg_file_name"
286+
sed -i "${serverNumber}s/.*/$name,$ip,$port,$user,$keyfile,$option/" "$cfg_file_name"
273287
printf "%s${success}SSH Connection has been edited${reset}\n"
274288
menu
275289

@@ -278,22 +292,21 @@ fi
278292

279293
}
280294

281-
listSSHCredentials(){
295+
listSSHCredentials(){
282296
fileEmptyCheck
283297
printf "%s${info}===========================${reset}\n"
284298
echo -e "${info} Saved SSH Connections ${reset}"
285299
printf "%s${info}===========================${reset}\n"
286300
#Now to use awk to list the servers in a nice format 1 , 2 , 3 etc in a table format starting with the header but starting the numbering at from the second line
287-
printf "%s${info}# Name IP/Host Port Username Key file${reset}\n"
288-
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works
301+
printf "%s${info}# Name IP/Host Port Username Key file Option${reset}\n"
302+
# -t is used to align the columns, using awk is always awkward .... but it works
303+
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6}' "$cfg_file_name" | column -t
289304

290305
menu
291306

292-
293-
294307
}
295308

296-
connectToSSHServer(){
309+
connectToSSHServer(){
297310

298311
if [ "$1" == "qc" ] #qc is the quick connect option
299312
then
@@ -303,6 +316,7 @@ fi
303316
serverPort=$port
304317
serverUser=$user
305318
serverKeyFile=$keyfile
319+
serverOpttion=$option
306320
else
307321

308322
fileEmptyCheck
@@ -311,7 +325,7 @@ fi
311325
printf "%s${info}===========================${reset}\n"
312326
#Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
313327
printf "%s${info}# Name \t IP/Host \tPort Username\tKey file${reset}\n"
314-
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works
328+
awk -F, '{print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works
315329

316330
printf "%s${info}Enter the number of the SSH connection you want to connect to or enter 0 to cancel : ${reset}"
317331
read -p "" serverNumber
@@ -344,17 +358,22 @@ fi
344358
serverPort=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
345359
serverUser=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $4}' "$cfg_file_name")
346360
serverKeyFile=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $5}' "$cfg_file_name")
347-
361+
serverOption=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $6}' "$cfg_file_name")
362+
echo "Option=$serverOption"
348363

349364
fi
350365
#echo $serverPort
351366
#Here we go connecting to the server
352367
printf "%s${success}Connecting to ${serverName} ...${reset}\n"
353368
if [ -z $serverKeyFile ]
354369
then
355-
ssh -p "$serverPort" "$serverUser""@""$serverIp"
370+
commande="ssh -p "$serverPort" "$serverUser""@""$serverIp" "$serverOption""
371+
#echo $commande
372+
$commande
356373
else
357-
ssh -i "$serverKeyFile" -p "$serverPort" "$serverUser""@""$serverIp"
374+
commande="ssh -i "$serverKeyFile" -p "$serverPort" "$serverUser""@""$serverIp" "$serverOption""
375+
#echo $commande
376+
$commande
358377
fi
359378
menu
360379

@@ -413,20 +432,20 @@ fi
413432
printf "%s${info}===========================${reset}\n"
414433
printf "%s${success}SSH Manager${reset}\n"
415434
printf "%s${info}===========================${reset}\n"
416-
printf "1. Add new SSH connection \n"
435+
printf "1. List Saved SSH connections \n"
417436
printf "2. Connect to a saved SSH connection \n"
418-
printf "3. Edit a saved SSH connection \n"
419-
printf "4. List Saved SSH connections \n"
437+
printf "3. Add new SSH connection \n"
438+
printf "4. Edit a saved SSH connection \n"
420439
printf "%s${warning}5. Delete a saved SSH connection ${reset}\n"
421440
printf "6. Exit\n"
422441
printf "Enter your choice [1-6] : "
423442
read -p "" choice
424443

425444
case $choice in
426-
1) createNewSSHCredentials;;
445+
1) listSSHCredentials;;
427446
2) connectToSSHServer;;
428-
3) editSSHConnection;;
429-
4) listSSHCredentials;;
447+
3) createNewSSHCredentials;;
448+
4) editSSHConnection;;
430449
5) deleteSSHServer;;
431450
6) exit;;
432451
*) printf "%s${warning}Invalid choice${reset}\n"; menu;;

0 commit comments

Comments
 (0)