3

「aws ec2」を使用して特定のポートのすべてのルールを削除する方法は?

aws ec2 revoke-security-group-ingress --group-name MySecurityGroup --protocol tcp --port 22 **--ALL-IP**
4

3 に答える 3

3

ドキュメントによると、このコマンドは または のいずれ--cidrかで機能します--source-group。したがって、複数の IP アドレスがある場合、唯一のオプションは、個々の IP アドレスに対して同じコマンドを複数回実行することです (これは の形式になります1.1.1.1/32)。

または、

すべての ipadres を cidr 形式 (1.1.1.1/32) でファイルに一覧表示し (各 IP アドレスを新しい行に記述)、for上記のコマンドを繰り返し実行するたびにループを実行できます。例えば

for i in `cat ip_address_cidr.txt`; do aws ec2 revoke-security-group-ingress --group-name MySecurityGroup --protocol tcp --port 22 $i; done

上記のコマンド構文はテストしていませんが、単一のワンライナー コマンドでルールを取り消すことができるようにする必要があります。

于 2013-10-24T04:25:07.973 に答える
1

これがあなたが探しているものだと思います: AWSセキュリティグループで開いているすべてのSSHポートを閉じる方法

特定のセキュリティ グループ ID のソリューションを次に示します。

#!/bin/bash

sg = {security group}

# get the cidrs for the ingress rule
rules=$(aws ec2 describe-security-groups --group-ids $sg --output text --query 'SecurityGroups[*].IpPermissions')
# rules will contain something like:
# 22 tcp 22
# IPRANGES 108.42.177.53/32
# IPRANGES 10.0.0.0/16
# 80 tcp 80
# IPRANGES 0.0.0.0/0
# luckily, aws returns all ipranges per port grouped together

# flag for if we are reading ipranges
reading=0
# loop returned lines
while read -r line; do
    # split the line up
    rulebits=($line)
    # check if if we are reading ssh port ipranges
    if [ $reading -eq 0 ] ; then
        # we are not reading ipranges
        # check if '22 tcp 22'
        if [ ${rulebits[0]} == "22" ] && [ ${rulebits[1]} == "tcp" ] && [ ${rulebits[2]} == "22" ] ; then
            # found it
            reading=1           
        fi
    else
        # we are reading ipranges
        # check if first word is 'IPRANGES'
        if [ ${rulebits[0]} == "IPRANGES" ] ; then
            # found a cidr for open ssh port
            cidr=${rulebits[1]}
            echo -n found port 22 open cidr $cidr closing...
            # close it
            result=$(aws ec2 revoke-security-group-ingress --group-id $sg --protocol tcp --port 22 --cidr $cidr --output text)
            if [ "$result" == "true" ] ; then
                echo " OK"
            else
                echo " ERROR"
            fi
        else
            # new port
            reading=0       
        fi
    fi
done
于 2015-05-21T17:59:23.040 に答える