【问题标题】:Adding the SNS topic to python script to send email将 SNS 主题添加到 python 脚本以发送电子邮件
【发布时间】:2018-09-30 19:16:07
【问题描述】:

我有一个 python 脚本,它在 AWS 中提供了最后一次登录的 IAM 用户名。我得到了预期的结果。现在我添加了 SN​​S 主题以通过电子邮件发送结果。 但我在电子邮件中收到的回复如下

回复:

User not logged into AWS: 

u

s

e

r

n

a

m

e

:

我需要在单行中作为用户名:“*****”

PhysicalString =''
for user in resource.users.all():
    if user.password_last_used is not None:
        delta = (today - user.password_last_used.replace(tzinfo=None)).days
            final_result=("username: ", [user.user_name][0]," - ",delta , "days")
            physicalString = 'User not logged into AWS: \n\n' + '\n'.join(str(final_result))
            response = sns.publish(
            TopicArn='*************',
            Message= physicalString,
            Subject='IAM USER',
            )
            return final_result

【问题讨论】:

    标签: python python-3.x amazon-web-services aws-lambda


    【解决方案1】:

    这个:

    '\n'.join(str(final_result))
    

    是说“将final_result 中的每个字符放在它们之间并在它们之间放置一个\n

    问题在于final_result 正在被转换为字符串,因此连接是针对每个字符而不是每个元素进行的。

    试试:

    '\n'.join(final_result)
    

    但您需要先将delta 转换为str(delta),然后再将其存储到final_result

    【讨论】:

    • 我修改了脚本如下
    • 我已将物理字符串行修改为“physicalString = '用户未登录 AWS:\n\n' + ''.join(final_result)”。现在我得到了预期的输出。 @John 感谢您的支持。
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2014-06-26
    • 2012-06-02
    • 2013-01-01
    • 2011-09-16
    • 2021-11-17
    相关资源
    最近更新 更多