-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateKeys.sh
executable file
·53 lines (41 loc) · 1.16 KB
/
createKeys.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# Creates and downloads key pairs
main(){
local force=false
while test $# != 0
do
case "$1" in
-f|--force) force=true ;;
--) shift; break;;
esac
shift
done
local MACHINE_SN=`system_profiler SPHardwareDataType | awk '/Serial\ Number/ {print $4}'`
[[ -z ${MACHINE_SN} ]] && exit 1
local COLOR_RED=$'\e[1;31m'
local RESET_COLOR=$'\e[0m'
KEY_STOCKHOLM="key-stockholm-${MACHINE_SN}"
KEY_FRANKFURT="key-frankfurt-${MACHINE_SN}"
! ${force} && [[ -f "${KEY_STOCKHOLM}.pem" ]] &&
ls -all "${KEY_STOCKHOLM}.pem" &&
echo "${COLOR_RED}The key file ${KEY_STOCKHOLM}.pem for Stockholm already exists!${RESET_COLOR}" &&
exit 1
! ${force} && [[ -f "${KEY_FRANKFURT}.pem" ]] &&
ls -all ${KEY_FRANKFURT_FILE} &&
echo "${COLOR_RED}The key file ${KEY_FRANKFURT}.pem for Frankfurt already exists!${RESET_COLOR}" &&
exit 1
${force} && rm -rf *.pem
createKey "eu-north-1" ${KEY_STOCKHOLM}
createKey "eu-central-1" ${KEY_FRANKFURT}
chmod 600 *.pem
}
createKey(){
aws ec2 create-key-pair \
--region ${1} \
--key-name ${2} \
--key-type rsa \
--query "KeyMaterial" \
--o text > "${2}.pem" ||
exit 1
}
./checkAWS.sh && main ${@} || ./awsCliNa.sh