forked from h3poteto/zsh-ec2ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-ec2ssh.zsh
146 lines (130 loc) · 4.71 KB
/
zsh-ec2ssh.zsh
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
139
140
141
142
143
144
145
146
function _load_aws_profile() {
local aws_profile=$1
if [ -z "${aws_profile}" ]; then
aws_profile=$AWS_DEFAULT_PROFILE
fi
echo $aws_profile
return
}
function _load_aws_region() {
local aws_region=$1
if [ -z "${aws_region}" ]; then
aws_region=$AWS_DEFAULT_REGION
fi
echo $aws_region
return
}
function _load_user() {
local user=$1
if [ -z "${user}" ]; then
user=$USER
fi
echo $user
return
}
function _load_ssh_private_key_path() {
local private_key_path=$1
if [ -z "${private_key_path}" ]; then
private_key_path="$HOME/.ssh/id_rsa"
fi
echo $private_key_path
return
}
function _load_port() {
local port=$1
if [ -z "${port}" ]; then
port=22
fi
echo $port
return
}
function zsh-ec2ssh() {
local aws_profile_name=$1
local aws_region=$2
local target_user=$3
local target_private_key_path=$4
local target_port=$5
local proxy_host=$6
local proxy_user=$7
local proxy_key_path=$8
local proxy_port=$9
aws_profile_name=`_load_aws_profile $aws_profile_name`
aws_region=`_load_aws_region $aws_region`
target_user=`_load_user $target_user`
target_private_key_path=`_load_ssh_private_key_path $target_private_key_path`
target_port=`_load_port $target_port`
proxy_user=`_load_user $proxy_user`
proxy_key_path=`_load_ssh_private_key_path $proxy_key_path`
proxy_port=`_load_port $proxy_port`
if [ -z "${aws_profile_name}" ]; then
echo "AWS profile name is required. Please call this function with aws profile name or set AWS_DEFAULT_REGION in evironment variables."
return
fi
if [ -z "${aws_region}" ]; then
echo "AWS region is required. Please call this function with aws region or set AWS_DEFAULT_REGION in environment variables."
return
fi
if [ -z "${target_user}" ]; then
echo "User is required. Please call this function with user or set USER in environment variables."
return
fi
echo "Fetching ec2 host..."
local selected_host=$(myaws ec2 ls --profile=${aws_profile_name} --region=${aws_region} --fields='InstanceId PublicIpAddress PrivateIpAddress LaunchTime Tag:Name' | sort -k5 | peco | cut -f3)
if [ -n "${selected_host}" ]; then
if [ -z "${proxy_host}" ]; then
BUFFER="ssh -i ${target_private_key_path} -p ${target_port} ${target_user}@${selected_host} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
else
BUFFER="ssh -i ${proxy_key_path} -p ${proxy_port} -t ${proxy_user}@${proxy_host} ssh ${target_user}@${selected_host} -i ${target_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
fi
if zle; then
zle accept-line
else
print -z "$BUFFER"
fi
fi
if zle; then
zle clear-screen
fi
}
zle -N zsh-ec2ssh
function zsh-ec2ssh-with-proxy() {
local aws_profile_name=$1
local aws_region=$2
local target_user=$3
local target_private_key_path=$4
local target_port=$5
local proxy_profile=$6
local proxy_user=$7
local proxy_key_path=$8
local proxy_port=$9
aws_profile_name=`_load_aws_profile $aws_profile_name`
aws_region=`_load_aws_region $aws_region`
target_user=`_load_user $target_user`
target_private_key_path=`_load_ssh_private_key_path $target_private_key_path`
target_port=`_load_port $target_port`
proxy_profile=`_load_aws_profile $proxy_profile`
proxy_user=`_load_user $proxy_user`
proxy_key_path=`_load_ssh_private_key_path $proxy_key_path`
proxy_port=`_load_port $proxy_port`
if [ -z "${aws_profile_name}" -o -z "${proxy_profile}" ]; then
echo "AWS profile name is required. Please call this function with aws profile name or set AWS_DEFAULT_REGION in evironment variables."
return
fi
if [ -z "${aws_region}" ]; then
echo "AWS region is required. Please call this function with aws region or set AWS_DEFAULT_REGION in environment variables."
return
fi
if [ -z "${target_user}" -o -z "${proxy_user}" ]; then
echo "User is required. Please call this function with user or set USER in environment variables."
return
fi
echo "Fetching ec2 host..."
local selected_proxy=$(myaws ec2 ls --profile=${proxy_profile} --region=${aws_region} --fields='InstanceId PublicIpAddress LaunchTime Tag:Name Tag:attached_asg' | sort -k4 | peco | cut -f2)
if [ -n "${selected_proxy}" ]; then
zsh-ec2ssh $aws_profile_name $aws_region $target_user $target_private_key_path $target_port $selected_proxy $proxy_user $proxy_key_path $proxy_port
fi
if zle; then
zle clear-screen
fi
}
zle -N zsh-ec2ssh-with-proxy