"aws ec2"를 사용하여 주어진 포트에 대한 모든 규칙을 제거하는 방법은 무엇입니까?특정 포트에 대한 AWS 보안 그룹 지우기 규칙
aws ec2 revoke-security-group-ingress --group-name MySecurityGroup --protocol tcp --port 22 **--ALL-IP**
"aws ec2"를 사용하여 주어진 포트에 대한 모든 규칙을 제거하는 방법은 무엇입니까?특정 포트에 대한 AWS 보안 그룹 지우기 규칙
aws ec2 revoke-security-group-ingress --group-name MySecurityGroup --protocol tcp --port 22 **--ALL-IP**
는 documentation 당으로,이 --cidr
또는 --source-group
중 하나에 작품을 명령한다. 그래서 여러 개의 IP 주소를 가지고 있다면, 유일한 옵션은 개별 IP 주소에 대해 동일한 명령을 여러 번 실행하는 것입니다 (이는 1.1.1.1/32
의 형태를 취할 것입니다).
또는
당신은 파일에 (새로운 행에 각 IP 주소)를 CIDR 형식 (1.1.1.1/32)의 모든 ipadress을 나열하고 다음을 위해 위의 명령 실행을 통해 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
위의 명령 구문은 테스트하지 않았지만 하나의 one-liner 명령으로 규칙을 취소 할 수 있습니다. 여기 How to Close All Open SSH Ports in AWS Security Groups
특정 보안 그룹 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
마지막 행은 다음과 같아야합니다 :'done <<< "$ rules"' – saq
감사합니다,하지만 난 읽고 어떻게 든하려고합니다 그룹의 설명 및 결과를 반복합니다. – Talma