-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-login-aws.sh
executable file
·138 lines (118 loc) · 3.86 KB
/
ssh-login-aws.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/commons/bootstrap.sh" && set +e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/commons/log.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/commons/colors.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/commons/assert.sh"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/commons/checks.sh"
function usage {
log_info "${magenta}Usage:${yellow} ./${0##*/} \$1(ssh user) \$2(host address||server ip) \$3(region) ${nocolor}\n"
}
function verify_args {
ssh_user="${1:-default}"
address="${2:-default}"
region="${3:-default}"
if [[ "$#" == 3 ]] && [[ "$ssh_user" != "default" ]] && [[ "$address" != "default" ]] && [[ "$region" != "default" ]]; then
return 0
else
log_error "${red}Arguments have not been set, check usage${nocolor}\n" && usage
return 1
fi
}
function is_address_valid {
local -r host=$address
if [[ "$host" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]] || [[ "$host" =~ ^(.*)\.amazonaws\.com$ ]]; then
return 0
else
log_error "${red}Invalid IP address or hostname, please enter EC2 IPv4 Public IP or EC2 Public DNS\n${nocolor}"
return 1
fi
}
function assert_ssh_user {
for user in $ssh_user
do
case "$user" in
u|ubuntu|ub )
ssh_user=ubuntu
;;
d|debian|deb )
ssh_user=admin
;;
c|centos|cent )
ssh_user=centos
;;
a|amazon|ec2 )
ssh_user=ec2-user
;;
*)
log_warn "${nocolor}${red}Invalid operating system input, please choose between:
- for Ubuntu: u|ubuntu|ub
- for Centos: c|centos|cent
- for Debian: d|debian|deb
- for Amazon Linux: a|amazon|ec2 \n${nocolor}"
return 1
;;
esac
done
}
function assert_region_key {
local -r key_us_east="~/path/to/key"
local -r key_us_west="~/path/to/key"
local -r key_europe="~/path/to/key"
for key in $region
do
case "$key" in
v|virg|virginia|us-east|useast|us-east-1|us-east-2|east|o|oh|ohio )
region_key=$key_us_east
aws_region=us-east
;;
w|west|ore|oregon|us-west|uswest|us-west-1|us-west-2|cali|california )
region_key=$key_us_west
aws_region=us-west
;;
e|eur|europe|germany|frankfurt|fra|eucentral|eu-central-1 )
region_key=$key_europe
aws_region=eu-central
;;
*)
log_warn "${nocolor}${red}Invalid region input, please choose:
- for Us-East: v|virg|virginia|us-east|useast|us-east-1|us-east-2|east|o|oh|ohio
- for Us-West: w|west|ore|oregon|us-west|uswest|us-west-1|us-west-2|cali|california
- for Eu-Central: e|eur|europe|germany|frankfurt|fra|eucentral|eu-central-1 \n${nocolor}"
return 1
;;
esac
done
}
function connect_ssh {
local -r option="StrictHostKeyChecking=no"
local -r timeout="ConnectTimeout=7"
log_info "${yellow}Connecting to EC2 instance${nocolor} ${blue}'"$address"'${nocolor} in ${magenta}'"$aws_region"'${nocolor} with user ${green}'"$ssh_user"'${nocolor}\n"
ssh -i ${region_key} -o "${no_strict}" -o "${timeout}" ${ssh_user}@${address}
}
function main {
echo -e "${magenta}"
echo -e "========================================="
echo -e "|Checking arguments & validating address|"
echo -e "========================================="
echo -e "${nocolor}"
verify_args "$@"
operation_result "checking provided arguments"
is_address_valid
operation_result "validating provided host's server IP or hostname"
echo -e "${magenta}"
echo -e "======================="
echo -e "|Verifying OS & region|"
echo -e "======================="
echo -e "${nocolor}"
assert_ssh_user
operation_result "defining default ssh_user"
assert_region_key
operation_result "determining key pair for region"
echo -e "${magenta}"
echo -e "==================="
echo -e "|Connecting via SSH|"
echo -e "==================="
echo -e "${nocolor}"
connect_ssh
}
main "$@"