0

i am new at this and here is a piece my code:

with open('Accounts.txt') as f:
    account_list = f.readlines()
    for account in account_list:
        while account_list:
            try:
                account = account.rstrip('\n') #strip end of account in accounts.txt
                assume_response = sts_default_role.assume_role(
                RoleArn = f'arn:aws:iam::{account}:role/user/user.infosec',
                RoleSessionName = 'test_session',
                )
                print(f"Logged in to {account}"),
                print("test first short loop")
                break
            except ClientError:
                print(f"Couldn't login to {account}"),
                break
                assume_creds = assume_response['Credentials']
                session = boto3.session.Session(
                aws_access_key_id=assume_creds['AccessKeyId'],
                aws_secret_access_key=assume_creds['SecretAccessKey'],
                aws_session_token=assume_creds['SessionToken'],
                )
        print("test outside the loop")

Here is my output:

Logged in to 733443824660
test first short loop
test outside the loop
Couldn't login to 111111222211
test outside the loop

as you can see it works great, the only problem i have is that once i hit an exception where i cant log in to an account i do not want the code to go further, because it does not make sense for it to go further and print (test outside the loop) comment when you cant log in to the account.

Any thoughts?

2
  • Does this answer your question? How to break out of multiple loops? Commented Mar 27, 2020 at 21:17
  • use exit() to break from the program Commented Mar 28, 2020 at 1:38

2 Answers 2

0

If you would like to break execution of this program when you can not login into account, then it's better to exit:

import sys
sys.exit()

This command will exit from your python script.

break command will only break execution of the inner loop. To break the outer loop, you will have to use break command again.

But if you can not login into one account from the list, maybe you will be able to login to any of the other ones.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Anastasiia, but:
When i add sys.exit() see below, my output is still not the same as i wanted to be:
try: account = account.rstrip('\n') #strip end of account in accounts.txt assume_response = sts_default_role.assume_role( RoleArn = f'arn:aws:iam::{account}:role/user/user.infosec', RoleSessionName = 'test_session', ) print(f"Logged in to {account}"), print("test first short loop") break except ClientError: print(f"Couldn't login to {account}"), sys.exit()
Logged in to 733447824660 test first short loop Couldn't login to 111111222211
you are right if i cant log in to account i want to proceed to try to log in to next account
|
0

Can suggest to remove second loop to loop through all accounts in the list:

with open('Accounts.txt') as f:
    account_list = f.readlines()
    for account in account_list:
        try:
            account = account.rstrip('\n') #strip end of account in accounts.txt
            assume_response = sts_default_role.assume_role(
            RoleArn = f'arn:aws:iam::{account}:role/user/user.infosec',
            RoleSessionName = 'test_session',
            )
            print(f"Logged in to {account}"),
            print("test first short loop")
            continue # to go to the next round of the loop
        except ClientError:
            print(f"Couldn't login to {account}"),
            break
            # this block will be not executed
        print("test outside the loop") # will not be called if exception occurs

2 Comments

"Logged in to 733447824660 test first short loop Couldn't login to 111111222211" Not sure that this is what we want.
Unfortunately, I have no idea how to fix your code without knowing what should it do and how should it work. If my answers are not working for you and you would like to get more help, please specify how exactly should your code work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.