|
164 | 164 |
|
165 | 165 | }
|
166 | 166 |
|
| 167 | +addNewServerKeyFile(){ |
| 168 | + |
| 169 | + |
| 170 | +read -p "Enter the path to the key file or leave blank if not used: " keyfile |
| 171 | + |
| 172 | +if ! [ -z "$keyfile" ] |
| 173 | +then |
| 174 | + #check if the key file exists |
| 175 | + if ! [ -f "$keyfile" ] |
| 176 | + then |
| 177 | + printf "%s${warning}File does not exist.${reset}\n" |
| 178 | + addNewServerKeyFile |
| 179 | + fi |
| 180 | +fi |
| 181 | +} |
| 182 | + |
| 183 | + editServerKeyFile(){ |
| 184 | + |
| 185 | + printf "Current key file is %s \n " "$keyfile" |
| 186 | + read -p "Enter the path to the key file or leave blank to use current key file: " newKeyFile |
| 187 | + |
| 188 | + if [ -z "$newKeyFile" ] |
| 189 | + then |
| 190 | + keyfile=${keyfile//\//\\/} |
| 191 | + else |
| 192 | + #check if the key file exists |
| 193 | + if ! [ -f "$newKeyFile" ] |
| 194 | + then |
| 195 | + printf "%s${warning}File does not exist.${reset}\n" |
| 196 | + editServerKeyFile |
| 197 | + else |
| 198 | + keyfile=${newKeyFile//\//\\/} |
| 199 | + fi |
| 200 | + fi |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + |
167 | 205 | createNewSSHCredentials(){
|
168 | 206 |
|
169 | 207 | printf "%s${info}===========================${reset}\n"
|
|
174 | 212 | addNewServerIp
|
175 | 213 | addNewServerPort
|
176 | 214 | addNewServerUser
|
| 215 | + addNewServerKeyFile |
177 | 216 |
|
178 | 217 |
|
179 |
| - echo "$name,$ip,$port,$user" >> "$cfg_file_name" |
| 218 | + echo "$name,$ip,$port,$user,$keyfile" >> "$cfg_file_name" |
180 | 219 |
|
181 | 220 | echo -e "${success}SSH Connection added successfully${reset}"
|
182 | 221 | read -p "Do you want to connect to the added SSH connection now? (y/n) " selection
|
|
196 | 235 | echo -e "${info} Saved SSH Connections ${reset}"
|
197 | 236 | printf "%s${info}===========================${reset}\n"
|
198 | 237 | #Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
|
199 |
| - printf "%s${info}# Name IP/Host \tPort Username${reset}\n" |
200 |
| - awk -F, '{print NR " " $1 " " $2 " " $3 " " $4}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works |
| 238 | + 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 |
201 | 240 |
|
202 | 241 | printf "%s${warning}Enter the number of the SSH connection you want to edit or enter 0 to cancel : ${reset}"
|
203 | 242 | read -p "" serverNumber
|
|
220 | 259 | ip=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $2}' "$cfg_file_name")
|
221 | 260 | port=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
|
222 | 261 | user=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $4}' "$cfg_file_name")
|
| 262 | + keyfile=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $5}' "$cfg_file_name") |
223 | 263 |
|
224 | 264 | editServer
|
225 | 265 | editServerIp
|
226 | 266 | editServerPort
|
227 | 267 | editServerUser
|
| 268 | + editServerKeyFile |
228 | 269 |
|
229 | 270 | #Now to replace the selected lines info with the updated info to the file
|
230 | 271 |
|
231 |
| - sed -i "${serverNumber}s/.*/$name,$ip,$port,$user/" "$cfg_file_name" |
| 272 | + sed -i "${serverNumber}s/.*/$name,$ip,$port,$user,$keyfile/" "$cfg_file_name" |
232 | 273 | printf "%s${success}SSH Connection has been edited${reset}\n"
|
233 | 274 | menu
|
234 | 275 |
|
|
243 | 284 | echo -e "${info} Saved SSH Connections ${reset}"
|
244 | 285 | printf "%s${info}===========================${reset}\n"
|
245 | 286 | #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
|
246 |
| - printf "%s${info}# Name IP/Host Port Username${reset}\n" |
247 |
| - awk -F, '{print NR " " $1 " " $2 " " $3 " " $4}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works |
| 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 |
248 | 289 |
|
249 | 290 | menu
|
250 | 291 |
|
|
261 | 302 | serverIp=$ip
|
262 | 303 | serverPort=$port
|
263 | 304 | serverUser=$user
|
| 305 | + serverKeyFile=$keyfile |
264 | 306 | else
|
265 | 307 |
|
266 | 308 | fileEmptyCheck
|
267 | 309 | printf "%s${info}===========================${reset}\n"
|
268 | 310 | echo -e "${info} Saved SSH Connections ${reset}"
|
269 | 311 | printf "%s${info}===========================${reset}\n"
|
270 | 312 | #Now to use awk to list the servers in a nice format 1 , 2 , 3 etc
|
271 |
| - printf "%s${info}# Name \t IP/Host \tPort Username${reset}\n" |
272 |
| - awk -F, '{print NR " " $1 " " $2 " " $3 " " $4}' "$cfg_file_name" | column -t # -t is used to align the columns, using awk is always awkward .... but it works |
| 313 | + 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 |
273 | 315 |
|
274 | 316 | printf "%s${info}Enter the number of the SSH connection you want to connect to or enter 0 to cancel : ${reset}"
|
275 | 317 | read -p "" serverNumber
|
|
301 | 343 | serverIp=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $2}' "$cfg_file_name")
|
302 | 344 | serverPort=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $3}' "$cfg_file_name")
|
303 | 345 | serverUser=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $4}' "$cfg_file_name")
|
| 346 | + serverKeyFile=$(awk -F, -v serverNumber="$serverNumber" 'NR==serverNumber {print $5}' "$cfg_file_name") |
304 | 347 |
|
305 | 348 |
|
306 | 349 | fi
|
307 | 350 | #echo $serverPort
|
308 | 351 | #Here we go connecting to the server
|
309 | 352 | printf "%s${success}Connecting to ${serverName} ...${reset}\n"
|
310 |
| - ssh -p "$serverPort" "$serverUser""@""$serverIp" |
311 |
| - |
| 353 | + if [ -z $serverKeyFile ] |
| 354 | + then |
| 355 | + ssh -p "$serverPort" "$serverUser""@""$serverIp" |
| 356 | + else |
| 357 | + ssh -i "$serverKeyFile" -p "$serverPort" "$serverUser""@""$serverIp" |
| 358 | + fi |
312 | 359 | menu
|
313 | 360 |
|
314 | 361 |
|
|
0 commit comments