Last active
February 6, 2020 10:23
-
-
Save mguilherme/54fc8b5b3702d69b512652888b356c97 to your computer and use it in GitHub Desktop.
Automate the AWS MFA code when running an AWS command
This file contains 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
# $ 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