【问题标题】:Add a list of IP as Source IP in Security Group在安全组中添加 IP 列表作为源 IP
【发布时间】:2022-01-11 13:48:09
【问题描述】:

我有一个 IP 地址列表。我想允许来自他们的tcp/22 流量并阻止任何其他IP 地址。该列表很长,包含大约 50-60 个 IP 地址。如何在不手动添加的情况下将其添加到安全组中。

谢谢!

解决方案:

我找到了一种使用aws cli的方法

允许 SSH 访问一系列 cidrs

bash# for ip in {csv_list_of_cidrs}
> do
> aws ec2 authorize-security-group-ingress --group-id <sg_ig_here> --protocol tcp --port 22 --cidr $ip
> done

例如:

for ip in {1.1.1.1/8,2.2.2.2/16,3.2.3.12/29}
do
aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxxxx --protocol tcp --port 22 --cidr $ip
done

使用boto3的解决方案参考下面的答案

【问题讨论】:

  • 这些 ips 是否存储在文件中?

标签: amazon-web-services aws-security-group


【解决方案1】:

这是一个你可以使用的 boto3 脚本:

    ec2_client = boto3.client('ec2')
    ec2_client.authorize_security_group_ingress(
        GroupId=group_id,
        IpPermissions=[
            {
                'IpProtocol': 'tcp',
                'FromPort': 22,
                'ToPort': 22,
                'IpRanges': [ip_list_here]
            }
        ])

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 2017-10-20
    • 2015-01-09
    • 1970-01-01
    • 2021-05-08
    • 2017-11-05
    • 2017-01-18
    • 1970-01-01
    • 2022-01-03
    相关资源
    最近更新 更多