mardi 29 octobre 2019

Run Python Script within Python & check if value is outputted - If statement

I have a Python3 script which basically runs through a list of Amazon AWS Account numbers (Uses Boto3), checks to see if their access keys are older than x number of days and report on it.

I'd like to make my report nice by checking to see if the output has a user(s) or not and output this into a file for SNS to email to me.

Here is the code I've already tried:

if not os.system("python3 ListUsersWithAccessKeysOlderThan90Days.py " + accountNumber):
            print("No Content", file=reportName)
        else:
            print("Content", file=reportName)

I've already tried this too:

        if os.system("python3 ListUsersWithAccessKeysOlderThan90Days.py " + accountNumber) == " ":
            print("No Content", file=reportName)
        else:
            print("Content", file=reportName)

But I only seem to get this in my output file:

Running on account accountNumber - accountLabel - accountEnvironment
No Content

Running on account accountNumber - accountLabel - accountEnvironment
No Content

Running on account accountNumber - accountLabel - accountEnvironment
No Content

Ideally, I'd like it to look like this:

Running on account accountNumber - accountLabel - accountEnvironment
No Content

Running on account accountNumber - accountLabel - accountEnvironment
Content

Running on account accountNumber - accountLabel - accountEnvironment
No Content

No Content = No access keys need rotating. Content = User needs their key rotating.

I can achieve this in Bash, but I wouldn't mind trying to get it working in Python3.

Here is my Bash example:

if [[ -z "$(python3 ListUsersWithAccessKeysOlderThan90Days.py ${ACCOUNT})" ]]; then
        echo -e "$ACCOUNT ($LABEL) is up to date no need to report\n" >> $REPORT
    else
    echo -e "$ACCOUNT Need keys rotating" >> $REPORT 
    fi

Any help would be most appreciated.

Thanks,

Aucun commentaire:

Enregistrer un commentaire