Created
December 24, 2015 09:11
-
-
Save yyuu/49f9fa40b2d559af1d14 to your computer and use it in GitHub Desktop.
A bash script to do grep against AWS IAM users
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
#!/usr/bin/env bash | |
set -e | |
AWSCLI_OPTIONS="" | |
tmpdir="$(mktemp -d "${TMP:-/tmp}/${BASH_SOURCE##*/}.XXXXXXXX")" | |
on_exit() { | |
rm -fr "${tmpdir}" | |
} | |
trap on_exit EXIT | |
if [ -z "$1" ]; then | |
echo "$0 [PATTERNS]..." 1>&2 | |
exit 1 | |
fi | |
list_users() { | |
aws ${AWSCLI_OPTIONS} iam list-users | \ | |
python -c 'from __future__ import print_function;import json,sys;print("\n".join({u["UserName"] for u in json.loads(sys.stdin.read())["Users"] if "UserName" in u}))' | |
} | |
cd "${tmpdir}" | |
for user in $(list_users); do | |
aws ${AWSCLI_OPTIONS} iam list-access-keys --user-name "${user}" > "${user}.json" | |
for arg; do | |
if grep -i -q "${arg}" "${user}.json" 1>/dev/null 2>&1; then | |
cat "${user}.json" | |
exit 0 | |
fi | |
done | |
done | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment