Skip to content

Instantly share code, notes, and snippets.

@joe41203
Last active June 24, 2019 08:01
Show Gist options
  • Save joe41203/33dd9944ebcc81212e08a333ef9af51f to your computer and use it in GitHub Desktop.
Save joe41203/33dd9944ebcc81212e08a333ef9af51f to your computer and use it in GitHub Desktop.
インベントリに登録されているEC2のカーネルのバージョンを取得するスクリプト
#!/bin/bash
#
set -e
function usage {
cat <<EOF
$(basename "$0") is a tool for fetching kernel version
Usage:
$(basename "$0") [command] [<options>]
Options:
--profile, -p Configured profile. You can find 'cat ~/.aws/config'
EOF
}
if [ "$#" -gt 2 ]; then
usage
exit 1
fi
POSITIONAL=()
while [ "$#" -gt 0 ]
do
key="$1"
case "${key}" in
-p|--profile)
PROFILE="$2"
shift
shift
;;
-h|--help)
usage
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
function __fetch_kernel_version () {
local _profile=$1
local _instance_id=$2
aws ssm list-inventory-entries \
--region ap-northeast-1 \
--profile $_profile \
--instance-id $_instance_id \
--type-name "AWS:Application" \
--filters "Key=Name,Values=Kernel" | jq -r ".Entries[].PackageId"
}
function __fetch_instance_ids () {
local _profile=$1
aws ssm get-inventory \
--region ap-northeast-1 \
--profile $_profile | grep InstanceId | grep -o 'i-[a-z0-9A-Z]*'
}
echo "profile, instance_id, kernel_version"
if [ -n "$PROFILE" ]; then
_instance_ids=$(__fetch_instance_ids $PROFILE)
for _id in ${_instance_ids}; do
_kernel_version=$(__fetch_kernel_version ${PROFILE} ${_id})
echo "${PROFILE}, ${_id}, ${_kernel_version}"
done
else
PROFILES=$(cat ~/.aws/config | grep profile | grep -v source | awk '{print $2}' | tr -d "]")
for _profile in ${PROFILES}; do
_instance_ids=$(__fetch_instance_ids $_profile)
for _id in ${_instance_ids}; do
_kernel_version=$(__fetch_kernel_version ${_profile} ${_id})
echo "${_profile}, ${_id}, ${_kernel_version}"
done
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment