-
-
Save drocco007/00158967572ef6a2ee8a17e56b12fa57 to your computer and use it in GitHub Desktop.
lsi: Tabular list of AWS EC2 instances (replaces awless ls instances)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
COLUMNS=8 | |
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" | |
# https://github.com/jakobwesthoff/prettytable.sh | |
if [ ! -s "$SCRIPT_DIR/prettytable.sh" ] ; then | |
# execute in a subshell so wget’s output isn’t fed into the printed table | |
$(wget -O "$SCRIPT_DIR/prettytable.sh" https://github.com/jakobwesthoff/prettytable.sh/raw/master/prettytable.sh) | |
fi | |
. "$SCRIPT_DIR/prettytable.sh" | |
humanize_uptime () { | |
# FIXME: honor columns variable | |
awk -v FS='\t' ' | |
{ | |
printf "%s", substr($0, 0, length($0) - length($8)) | |
if ($8 < 2) { | |
printf "1 sec" | |
} else if ($8 < 120) { | |
printf "%s secs", int($8) | |
} else if ($8 < 3600) { | |
printf "%s mins", int($8 / 60) | |
} else if ($8 < 7200) { | |
printf "1 hour" | |
} else if ($8 < 86400) { | |
printf "%s hours", int($8 / 3600) | |
} else if ($8 < 1209600) { /* 2 weeks in seconds */ | |
printf "%s days", int($8 / 86400) | |
} else if ($8 < 4838400) { /* 8 weeks in seconds */ | |
printf "%s weeks", int($8 / 604800) | |
} else if ($8 < 60480000) { /* 100 weeks in seconds */ | |
printf "%s months", int($8 / 2419200) | |
} else { | |
printf "%s years", int($8 / 31557600) | |
} | |
printf "\n" | |
} | |
' | |
} | |
header="ID ▲\tZONE\tNAME\tSTATE\tTYPE\tPUBLIC IP\tPRIVATE IP\tUPTIME" | |
instance_data=$(aws ec2 describe-instances --query Reservations[].Instances[]) | |
instances=$(echo "$instance_data" | | |
jq -r ' | |
.[] | |
| [ | |
.InstanceId, | |
.Placement.AvailabilityZone, | |
(.Tags[]|select(.Key == "Name")).Value // "—", | |
.State.Name, | |
.InstanceType, | |
.PublicIpAddress, | |
.PrivateIpAddress, | |
(now - (.LaunchTime | strptime("%Y-%m-%dT%H:%M:%S+00:00") | mktime)) | |
] | |
| @tsv' \ | |
| sort \ | |
| humanize_uptime | |
) | |
out=$( | |
echo -e "$header\n$instances" \ | |
| prettytable ${COLUMNS} \ | |
| egrep -v '^(┌|└)' \ | |
| sed 's/running/\\e[32mrunning\\e[0m/;s/stopped/\\e[31mstopped\\e[0m/' | |
# ^^^ quick & dirty colorizer; make better! | |
) | |
echo -e "$out" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: