Skip to content

Instantly share code, notes, and snippets.

@masami256
Created June 5, 2018 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masami256/2361e3df9474e443a8493fbd237b6f1f to your computer and use it in GitHub Desktop.
Save masami256/2361e3df9474e443a8493fbd237b6f1f to your computer and use it in GitHub Desktop.
AWSのコマンドラインインターフェースで2段階認証を使うための便利ツール
#!/bin/bash
# Before run this script, get authengication token via google authenticator app or other tool
if [ $# != 1 ]; then
echo "usage: $0 [token]"
exit 1
fi
tokencode=$1
which jq >/dev/null 2>&1
if [ $? != 0 ]; then
echo "jq command is required"
exit 1
fi
if [ "${MFA_AWS_DEVICE_ARN}" = "" ]; then
echo "please set MFA device's arn to MFA_AWS_DEVICE_ARN"
exit 1
fi
tmpfile=$(mktemp)
aws sts get-session-token --serial-number "${MFA_AWS_DEVICE_ARN}" --token-code "${tokencode}" > ${tmpfile}
if [ $? = 0 ]; then
echo "Please run following command."
echo "export AWS_ACCESS_KEY_ID=$(jq '.Credentials.AccessKeyId' ${tmpfile})"
echo "export AWS_SECRET_ACCESS_KEY=$(jq '.Credentials.SecretAccessKey' ${tmpfile})"
echo "export AWS_SESSION_TOKEN=$(jq '.Credentials.SessionToken' ${tmpfile})"
else
echo "failed to get session"
fi
rm -f ${tmpfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment