【问题标题】:How to iterate through security groups in AWS ec2?如何遍历 AWS ec2 中的安全组?
【发布时间】:2015-08-20 01:41:43
【问题描述】:

我正在尝试遍历我的 ec2 安全组。我编写了以下代码来连接并获取所有安全组:

security_groups = ec2_conn.get_all_security_groups()

下一步我只需要使用每个安全组中的组 ID。在我的 for 循环中,我使用此代码打印:

print(security_groups[group_id])

我尝试了许多不同的属性而不是 [group_id] 但我不断收到错误:

"NameError: name 'group_id' is not defined"

从安全组中仅检索或打印一个属性的正确方法是什么?

【问题讨论】:

    标签: python amazon-web-services amazon-ec2


    【解决方案1】:

    这是一个如何遍历安全组并打印其组 ID 的工作示例

    import boto
    ec2_conn = boto.connect_ec2()
    security_groups = ec2_conn.get_all_security_groups()
    for group in security_groups:
        print(group.id)
    

    有关操纵安全组的一些附加信息:http://boto.readthedocs.org/en/latest/security_groups.html

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2021-04-06
      • 2021-10-24
      • 2017-08-08
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多