-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-sm.sh
executable file
·164 lines (128 loc) · 4.32 KB
/
aws-sm.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
#Author: Quach Chi Cuong
set -e
usage_help()
{
cat <<HELP
Usage: aws-vault <command> [<args> ...]
Script help you to list ec2 instance with active ssm agent. Then you can connect ssh to EC2 Instance via Active Installed SSM Agent.
Available Commands:
list List all active ec2 instances in region.
connect Connect to EC2 Instance via active installed SSM Agent
Software dependencies:
- AWS CLI
- Session Manager Plugin for AWS CLI
- jq
Examples:
- aws-sm list
- aws-sm connect <instance_id>
HELP
exit 1
}
pre_check_dependencies(){
# If not found tool AWS CLI v1 : aws => exit
if [[ ! $(command -v aws) ]];then
echo "[x] Not found tool [aws] on machine."
echo "Exit."
exit 1
fi
# If not found session manager plugin for AWS CLI : session-manager-plugin => exit
if [[ ! $(command -v session-manager-plugin) ]];then
echo "[x] Not found tool [session-manager-plugin] on machine."
echo "Exit 1."
exit 1
fi
# If not found : jq => exit
if [[ ! $(command -v jq) ]];then
echo "[x] Not found tool [jq] on machine."
echo "Exit."
exit 1
fi
}
install_session_manager_plugin(){
# This function is only for note
# Only for macos
curl -L "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"
unzip sessionmanager-bundle.zip
sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin
}
list_ec2_instances()
{
# Get list ec2 instances and tag name
LIST_EC2_INSTANCES=$(aws ec2 describe-instances --filters "Name=instance-state-code,Values=16" --output json | jq '.Reservations[].Instances[]' | jq '{name: .Tags[] | select(.Key=="Name") | .Value, instance_id: .InstanceId, private_ip: .NetworkInterfaces [0].PrivateIpAddress}' | jq -r '[.instance_id, .private_ip, .name]|@tsv' | sort -k3,3)
# Get list ec2 instances with active installed ssm agent
LIST_ACTIVE_SSM_EC2_INSTANCES=$(aws ssm describe-instance-information | jq '.InstanceInformationList[] | select(.PingStatus=="Online")' | jq '{instance_id: .InstanceId}' | jq -r '[.instance_id]|@tsv')
# Determine ec2 has active or inactive/unknown ssm agent
{
echo -e "InstanceID\tStatus\tPrivateIP\tName"
echo ""
while IFS= read -r line; do
info_instance_id=$(echo $line | awk '{print $1}')
info_instance_meta_ip=$(echo $line | awk '{print $2}')
info_instance_meta_tagname=$(echo $line | awk '{print $3}')
# If found instance_id in list active ssm agent instances id
# So: that instance_id is active
if [[ $(grep -i ${info_instance_id} <<< ${LIST_ACTIVE_SSM_EC2_INSTANCES}) ]];then
status="active"
else
status="inactive"
fi
# Print out information
echo -e "${info_instance_id}\t${status}\t${info_instance_meta_ip}\t${info_instance_meta_tagname}"
done <<< "$LIST_EC2_INSTANCES"
} | column -t -s $'\t'
}
aws_ssm_connect_instance(){
aws ssm start-session --target $AWS_INSTANCE_ID
}
# Main functions
if [[ "$#" -lt 1 ]];then
echo -e "Error: missing arguments\n"
usage_help
fi
if [[ "$#" -gt 2 ]];then
echo -e "Error: over supported arguments\n"
usage_help
fi
if [[ "$#" -eq 1 ]];then
if [[ $1 == "help" ]];then
usage_help
fi
if [[ $1 == "connect" ]];then
echo -e "Error: are you missing args <instance_id> with command <connect> \n"
usage_help
fi
if [[ $1 != "list" ]];then
echo -e "Error: unsupported command <$1> \n"
usage_help
fi
fi
if [[ "$#" -eq 2 ]];then
if [[ $1 != "connect" ]];then
echo -e "Error: unsupported command <$1> \n"
usage_help
fi
if [[ ! $(echo $2 | grep "^i-") ]];then
echo -e "Error: wrong syntax aws <instance_id> with command <connect> \n"
usage_help
fi
fi
# Assign variables from args
OPTION="$1"
AWS_INSTANCE_ID="$2"
# Checking supported tool on local machine
pre_check_dependencies
# Action based on $OPTION arg
case $OPTION in
"list")
list_ec2_instances
;;
"connect")
aws_ssm_connect_instance
;;
*)
echo -n "Error: Something wrong"
usage_help
;;
esac
exit 0