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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

2 Answers2

1

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.

Anastasiia
  • 245
  • 1
  • 5
  • Thanks Anastasiia, but: – NewDeveloper Mar 29 '20 at 00:40
  • When i add sys.exit() see below, my output is still not the same as i wanted to be: – NewDeveloper Mar 29 '20 at 00:41
  • 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() – NewDeveloper Mar 29 '20 at 00:42
  • Logged in to 733447824660 test first short loop Couldn't login to 111111222211 – NewDeveloper Mar 29 '20 at 00:42
  • you are right if i cant log in to account i want to proceed to try to log in to next account – NewDeveloper Mar 29 '20 at 00:43
  • its like i want to do something like this (not sure how to do it though) If clienterror then try next account, else move on to the rest of the script – NewDeveloper Mar 29 '20 at 00:53
  • The first for loop is looping through accounts in account_list. Can you please explain what does while loop do? – Anastasiia Mar 29 '20 at 08:52
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
Anastasiia
  • 245
  • 1
  • 5
  • "Logged in to 733447824660 test first short loop Couldn't login to 111111222211" Not sure that this is what we want. – NewDeveloper Mar 30 '20 at 03:41
  • 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. – Anastasiia Mar 30 '20 at 14:10