Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active July 14, 2020 17:59
Show Gist options
  • Save jfeilbach/f238a468768a0fee1c72e5e954fe315a to your computer and use it in GitHub Desktop.
Save jfeilbach/f238a468768a0fee1c72e5e954fe315a to your computer and use it in GitHub Desktop.
Update all AWS tokens.
#!/bin/bash
# Update AWS tokens in all profiles
# Credentials File
creds="/home/${USER}/.aws/credentials"
# AWS username
fed=${FED_USERNAME}
# Convert UTC to local time
tz=$(timedatectl | grep "Time zone" | awk '{ print $4 " " $5 }')
# Get a list of profiles
profile=$(grep "\[*\]" "${creds}" | sed 's/[][]//g' | tr '\n' ' ')
# Set color output
NC='\033[0m' # No color
WHITE='\033[0;37m' # White
RED='\033[0;31m' # Red
CYAN='\033[0;36m' # Cyan
if [ -z "$fed" ]
then
echo -e "${RED}${fed} is null. Please set ${fed} Exiting.${NC}"
exit 1
fi
# Optional column header
#echo -e " Env \t Acct Exp Time Time"
#echo -e "======================================================="
for x in ${profile} ; do
SECONDS=0
fed up $x
acct_id=$(cat ${creds} | grep -A 11 "\[${x}\]" | grep "fed_role" | awk -F ":" '{ print $5 }')
exp=$(cat ${creds} | grep -A 11 "\[${x}\]" | grep fed_expire | awk '{ print $3}')
ts=$(date -d "$exp" '+%T')
echo -e "${WHITE} ${x}${NC}\t\t${CYAN}${acct_id}${NC} ${ts} ${tz} (${SECONDS} secs)"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment