Skip to content

Commit 4cb8368

Browse files
committed
test
1 parent 8e6a5fd commit 4cb8368

File tree

1 file changed

+115
-34
lines changed

1 file changed

+115
-34
lines changed

ssh-manager.sh

+115-34
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fi
148148

149149

150150

151-
}
151+
}
152152

153153
editServerUser(){
154154

@@ -162,7 +162,36 @@ editServerUser(){
162162
user=$newUser
163163
fi
164164

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+
}
166195

167196
addNewServerKeyFile(){
168197

@@ -178,6 +207,7 @@ then
178207
addNewServerKeyFile
179208
fi
180209
fi
210+
181211
}
182212

183213
editServerKeyFile(){
@@ -199,7 +229,7 @@ editServerKeyFile(){
199229
fi
200230
fi
201231

202-
}
232+
}
203233

204234
addNewServerOption(){
205235
read -p "Enter the options leave blank if not used: " option
@@ -210,7 +240,7 @@ editServerOption(){
210240

211241
printf "Current option is %s \n " "$option"
212242
read -p "Enter the path to the key file or leave blank to use current option: " newOption
213-
}
243+
}
214244

215245

216246
createNewSSHCredentials(){
@@ -223,11 +253,12 @@ createNewSSHCredentials(){
223253
addNewServerIp
224254
addNewServerPort
225255
addNewServerUser
256+
addNewServerPassword
226257
addNewServerKeyFile
227258
addNewServerOption
228259

229260

230-
echo "$name,$ip,$port,$user,$keyfile,$option," >> "$cfg_file_name"
261+
echo "$name,$ip,$port,$user,$password,$keyfile,$option," >> "$cfg_file_name"
231262

232263
echo -e "${success}SSH Connection added successfully${reset}"
233264
read -p "Do you want to connect to the added SSH connection now? (y/n) " selection
@@ -238,7 +269,7 @@ createNewSSHCredentials(){
238269

239270
menu
240271

241-
}
272+
}
242273

243274
editSSHConnection(){
244275

@@ -247,8 +278,8 @@ createNewSSHCredentials(){
247278
echo -e "${info} Saved SSH Connections ${reset}"
248279
printf "%s${info}===========================${reset}\n"
249280
#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
252283

253284
printf "%s${warning}Enter the number of the SSH connection you want to edit or enter 0 to cancel : ${reset}"
254285
read -p "" serverNumber
@@ -271,40 +302,81 @@ createNewSSHCredentials(){
271302
ip=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $2}' "$cfg_file_name")
272303
port=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
273304
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")
276308

277309
editServerName
278310
editServerIp
279311
editServerPort
280312
editServerUser
313+
editServerPassword
281314
editServerKeyFile
282315
editServerOption
283316

284317
#Now to replace the selected lines info with the updated info to the file
285318

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"
287320
printf "%s${success}SSH Connection has been edited${reset}\n"
288321
menu
289322

290323

291324

292325

293-
}
326+
}
294327

295328
listSSHCredentials(){
296329
fileEmptyCheck
297330
printf "%s${info}===========================${reset}\n"
298331
echo -e "${info} Saved SSH Connections ${reset}"
299332
printf "%s${info}===========================${reset}\n"
300333
#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"
302335
# -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
304375

305376
menu
377+
306378

307-
}
379+
}
308380

309381
connectToSSHServer(){
310382

@@ -315,6 +387,7 @@ connectToSSHServer(){
315387
serverIp=$ip
316388
serverPort=$port
317389
serverUser=$user
390+
serverPassword=$password
318391
serverKeyFile=$keyfile
319392
serverOpttion=$option
320393
else
@@ -324,8 +397,8 @@ connectToSSHServer(){
324397
echo -e "${info} Saved SSH Connections ${reset}"
325398
printf "%s${info}===========================${reset}\n"
326399
#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
329402

330403
printf "%s${info}Enter the number of the SSH connection you want to connect to or enter 0 to cancel : ${reset}"
331404
read -p "" serverNumber
@@ -357,29 +430,35 @@ connectToSSHServer(){
357430
serverIp=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $2}' "$cfg_file_name")
358431
serverPort=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
359432
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")
362436
echo "Option=$serverOption"
363437

364438
fi
365439
#echo $serverPort
366440
#Here we go connecting to the server
367441
printf "%s${success}Connecting to ${serverName} ...${reset}\n"
368-
if [ -z $serverKeyFile ]
442+
if [ ! -z $serverKeyFile ];
369443
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""
371450
#echo $commande
372451
$commande
373452
else
374-
commande="ssh -i "$serverKeyFile" -p "$serverPort" "$serverUser""@""$serverIp" "$serverOption""
453+
commande="ssh -p "$serverPort" "$serverUser""@""$serverIp" "$serverOption""
375454
#echo $commande
376455
$commande
377456
fi
378457
menu
379458

380459

381460

382-
}
461+
}
383462

384463
deleteSSHServer(){
385464

@@ -425,7 +504,7 @@ fi
425504
listSSHCredentials
426505

427506

428-
}
507+
}
429508

430509
menu(){
431510

@@ -434,24 +513,26 @@ fi
434513
printf "%s${info}===========================${reset}\n"
435514
printf "1. List Saved SSH connections \n"
436515
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] : "
442522
read -p "" choice
443523

444524
case $choice in
445525
1) listSSHCredentials;;
446526
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;;
451532
*) printf "%s${warning}Invalid choice${reset}\n"; menu;;
452533
esac
453534

454-
}
535+
}
455536

456537

457538

0 commit comments

Comments
 (0)