Skip to content

Instantly share code, notes, and snippets.

@mguilherme
Last active February 6, 2020 10:23
Show Gist options
  • Save mguilherme/54fc8b5b3702d69b512652888b356c97 to your computer and use it in GitHub Desktop.
Save mguilherme/54fc8b5b3702d69b512652888b356c97 to your computer and use it in GitHub Desktop.
Automate the AWS MFA code when running an AWS command
# $ pip3 install pexpect
# $ pip3 install pyotp
import os
import sys
import pexpect
import pyotp
os.environ['AWS_DEFAULT_PROFILE'] = 'admin' # Change this if you have a different profile
def main():
print('Entering MFA...')
enter_mfa()
print('Done')
def generate_code() -> str:
totp = pyotp.TOTP('#####') # Your MFA secret
return totp.now()
def enter_mfa():
child = pexpect.spawn('aws iam get-account-summary')
child.log_file_read = sys.stdout
child.expect('Enter MFA code for')
child.sendline(generate_code())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment