148
148
149
149
150
150
151
- }
151
+ }
152
152
153
153
editServerUser (){
154
154
@@ -162,7 +162,36 @@ editServerUser(){
162
162
user=$newUser
163
163
fi
164
164
165
- }
165
+ }
166
+
167
+ addNewServerPassword (){
168
+
169
+
170
+ read -p " Enter the password: " password
171
+
172
+ # Check if the user is blank
173
+ if [ -z " $password " ] # -z checks if the string is empty
174
+ then
175
+ printf " %s${warning} Password cannot be blank${reset} \n"
176
+ addNewServerPassword
177
+ fi
178
+
179
+
180
+ }
181
+
182
+ editServerPassword (){
183
+
184
+ printf " Current password is %s \n " " $password "
185
+ read -p " Enter the new password or leave blank to use current password : " newPassword
186
+
187
+ if [ -z " $newPassword " ]
188
+ then
189
+ user=$password
190
+ else
191
+ user=$newPassword
192
+ fi
193
+
194
+ }
166
195
167
196
addNewServerKeyFile (){
168
197
178
207
addNewServerKeyFile
179
208
fi
180
209
fi
210
+
181
211
}
182
212
183
213
editServerKeyFile (){
@@ -199,7 +229,7 @@ editServerKeyFile(){
199
229
fi
200
230
fi
201
231
202
- }
232
+ }
203
233
204
234
addNewServerOption (){
205
235
read -p " Enter the options leave blank if not used: " option
@@ -210,7 +240,7 @@ editServerOption(){
210
240
211
241
printf " Current option is %s \n " " $option "
212
242
read -p " Enter the path to the key file or leave blank to use current option: " newOption
213
- }
243
+ }
214
244
215
245
216
246
createNewSSHCredentials (){
@@ -223,11 +253,12 @@ createNewSSHCredentials(){
223
253
addNewServerIp
224
254
addNewServerPort
225
255
addNewServerUser
256
+ addNewServerPassword
226
257
addNewServerKeyFile
227
258
addNewServerOption
228
259
229
260
230
- echo " $name ,$ip ,$port ,$user ,$keyfile ,$option ," >> " $cfg_file_name "
261
+ echo " $name ,$ip ,$port ,$user ,$password , $ keyfile ,$option ," >> " $cfg_file_name "
231
262
232
263
echo -e " ${success} SSH Connection added successfully${reset} "
233
264
read -p " Do you want to connect to the added SSH connection now? (y/n) " selection
@@ -238,7 +269,7 @@ createNewSSHCredentials(){
238
269
239
270
menu
240
271
241
- }
272
+ }
242
273
243
274
editSSHConnection (){
244
275
@@ -247,8 +278,8 @@ createNewSSHCredentials(){
247
278
echo -e " ${info} Saved SSH Connections ${reset} "
248
279
printf " %s${info} ===========================${reset} \n"
249
280
# Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
250
- printf " %s${info} # Name IP/Host \tPort Username\tKey File${reset} \n"
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
281
+ printf " %s${info} # Name IP/Host \tPort Username\t Password\t Key File${reset} \n"
282
+ awk -F, ' {print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7 }' " $cfg_file_name " | column -t # -t is used to align the columns, using awk is always awkward .... but it works
252
283
253
284
printf " %s${warning} Enter the number of the SSH connection you want to edit or enter 0 to cancel : ${reset} "
254
285
read -p " " serverNumber
@@ -271,40 +302,81 @@ createNewSSHCredentials(){
271
302
ip=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $2}' " $cfg_file_name " )
272
303
port=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $3}' " $cfg_file_name " )
273
304
user=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $4}' " $cfg_file_name " )
274
- 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 " )
305
+ password=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $5}' " $cfg_file_name " )
306
+ keyfile=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $6}' " $cfg_file_name " )
307
+ option=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $7}' " $cfg_file_name " )
276
308
277
309
editServerName
278
310
editServerIp
279
311
editServerPort
280
312
editServerUser
313
+ editServerPassword
281
314
editServerKeyFile
282
315
editServerOption
283
316
284
317
# Now to replace the selected lines info with the updated info to the file
285
318
286
- sed -i " ${serverNumber} s/.*/$name ,$ip ,$port ,$user ,$keyfile ,$option /" " $cfg_file_name "
319
+ sed -i " ${serverNumber} s/.*/$name ,$ip ,$port ,$user ,$password , $ keyfile ,$option /" " $cfg_file_name "
287
320
printf " %s${success} SSH Connection has been edited${reset} \n"
288
321
menu
289
322
290
323
291
324
292
325
293
- }
326
+ }
294
327
295
328
listSSHCredentials (){
296
329
fileEmptyCheck
297
330
printf " %s${info} ===========================${reset} \n"
298
331
echo -e " ${info} Saved SSH Connections ${reset} "
299
332
printf " %s${info} ===========================${reset} \n"
300
333
# 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
301
- printf " %s${info} # Name IP/Host Port Username Key file Option${reset} \n"
334
+ printf " %s${info} # Name IP/Host Port Username Password Key file Option${reset} \n"
302
335
# -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
336
+ awk -F, ' {print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7}' " $cfg_file_name " | column -t
337
+
338
+ menu
339
+
340
+ }
341
+
342
+ SSHSearchCredential (){
343
+ fileEmptyCheck
344
+ printf " %s${info} ===========================${reset} \n"
345
+ echo -e " ${info} Saved SSH Connections ${reset} "
346
+ printf " %s${info} ===========================${reset} \n"
347
+ connection=" $( cat $cfg_file_name | fzf) "
348
+
349
+ serverName=$( echo $connection | awk -F, ' {print $1}' )
350
+ serverIp=$( echo $connection | awk -F, ' {print $2}' )
351
+ serverPort=$( echo $connection | awk -F, ' {print $3}' )
352
+ serverUser=$( echo $connection | awk -F, ' {print $4}' )
353
+ serverPassword=$( echo $connection | awk -F, ' {print $5}' )
354
+ serverKeyFile=$( echo $connection | awk -F, ' {print $6}' )
355
+ serverOption=$( echo $connection | awk -F, ' {print $7}' )
356
+
357
+ # echo $serverPort
358
+ # Here we go connecting to the server
359
+ printf " %s${success} Connecting to ${serverName} ...${reset} \n"
360
+ if [ ! -z $serverKeyFile ];
361
+ then
362
+ commande=" ssh -i " $serverKeyFile " -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
363
+ echo $commande
364
+ $commande
365
+ elif [ ! -z $serverPassword ];
366
+ then
367
+ commande=" sshpass -p " $serverPassword " ssh -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
368
+ echo $commande
369
+ $commande
370
+ else
371
+ commande=" ssh -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
372
+ echo $commande
373
+ $commande
374
+ fi
304
375
305
376
menu
377
+
306
378
307
- }
379
+ }
308
380
309
381
connectToSSHServer (){
310
382
@@ -315,6 +387,7 @@ connectToSSHServer(){
315
387
serverIp=$ip
316
388
serverPort=$port
317
389
serverUser=$user
390
+ serverPassword=$password
318
391
serverKeyFile=$keyfile
319
392
serverOpttion=$option
320
393
else
@@ -324,8 +397,8 @@ connectToSSHServer(){
324
397
echo -e " ${info} Saved SSH Connections ${reset} "
325
398
printf " %s${info} ===========================${reset} \n"
326
399
# Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
327
- printf " %s${info} # Name \t IP/Host \tPort Username\tKey file${reset} \n"
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
400
+ printf " %s${info} # Name \t IP/Host \tPort Username\t Password\t Key file${reset} \n"
401
+ awk -F, ' {print NR " " $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7 }' " $cfg_file_name " | column -t # -t is used to align the columns, using awk is always awkward .... but it works
329
402
330
403
printf " %s${info} Enter the number of the SSH connection you want to connect to or enter 0 to cancel : ${reset} "
331
404
read -p " " serverNumber
@@ -357,29 +430,35 @@ connectToSSHServer(){
357
430
serverIp=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $2}' " $cfg_file_name " )
358
431
serverPort=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $3}' " $cfg_file_name " )
359
432
serverUser=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $4}' " $cfg_file_name " )
360
- serverKeyFile=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $5}' " $cfg_file_name " )
361
- serverOption=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $6}' " $cfg_file_name " )
433
+ serverPassword=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $5}' " $cfg_file_name " )
434
+ serverKeyFile=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $6}' " $cfg_file_name " )
435
+ serverOption=$( awk -F, -v serverNumber=" $serverNumber " ' NR==serverNumber {print $7}' " $cfg_file_name " )
362
436
echo " Option=$serverOption "
363
437
364
438
fi
365
439
# echo $serverPort
366
440
# Here we go connecting to the server
367
441
printf " %s${success} Connecting to ${serverName} ...${reset} \n"
368
- if [ -z $serverKeyFile ]
442
+ if [ ! -z $serverKeyFile ];
369
443
then
370
- commande=" ssh -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
444
+ commande=" ssh -i " $serverKeyFile " -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
445
+ # echo $commande
446
+ $commande
447
+ elif [ ! -z $serverPassword ];
448
+ then
449
+ commande=" sshpass -p " $serverPassword " ssh -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
371
450
# echo $commande
372
451
$commande
373
452
else
374
- commande=" ssh -i " $serverKeyFile " - p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
453
+ commande=" ssh -p " $serverPort " " $serverUser " " @" " $serverIp " " $serverOption " "
375
454
# echo $commande
376
455
$commande
377
456
fi
378
457
menu
379
458
380
459
381
460
382
- }
461
+ }
383
462
384
463
deleteSSHServer (){
385
464
425
504
listSSHCredentials
426
505
427
506
428
- }
507
+ }
429
508
430
509
menu (){
431
510
434
513
printf " %s${info} ===========================${reset} \n"
435
514
printf " 1. List Saved SSH connections \n"
436
515
printf " 2. Connect to a saved SSH connection \n"
437
- printf " 3. Add new SSH connection \n"
438
- printf " 4. Edit a saved SSH connection \n"
439
- printf " %s${warning} 5. Delete a saved SSH connection ${reset} \n"
440
- printf " 6. Exit\n"
441
- printf " Enter your choice [1-6] : "
516
+ printf " 3. Search and connect to a saved SSH connection \n"
517
+ printf " 4. Add new SSH connection \n"
518
+ printf " 5. Edit a saved SSH connection \n"
519
+ printf " %s${warning} 6. Delete a saved SSH connection ${reset} \n"
520
+ printf " 7. Exit\n"
521
+ printf " Enter your choice [1-7] : "
442
522
read -p " " choice
443
523
444
524
case $choice in
445
525
1) listSSHCredentials;;
446
526
2) connectToSSHServer;;
447
- 3) createNewSSHCredentials;;
448
- 4) editSSHConnection;;
449
- 5) deleteSSHServer;;
450
- 6) exit ;;
527
+ 3) SSHSearchCredential;;
528
+ 4) createNewSSHCredentials;;
529
+ 5) editSSHConnection;;
530
+ 6) deleteSSHServer;;
531
+ 7) exit ;;
451
532
* ) printf " %s${warning} Invalid choice${reset} \n" ; menu;;
452
533
esac
453
534
454
- }
535
+ }
455
536
456
537
457
538
0 commit comments