【发布时间】:2023-03-27 10:40:01
【问题描述】:
我想在备份过程开始之前使用aws cli 工具临时打开某个IP地址的端口,并在完成后关闭它。
我知道如何通过控制台执行此操作,但我找不到如何以编程方式执行此操作。
有谁知道我可以运行什么命令来做到这一点?
我正在考虑编写一个 shell 脚本来执行此操作并在备份之前启动它,所以我找到了一个完全相同的 Circle CI Orb。但是,当我尝试使用 shell 脚本启动它时,会出现错误。我不太擅长 shell 命令,所以也许有人可以告诉我我可以在下面修复什么?
AWS 的权限设置正确,所以我想我只需要在下面的脚本中进行一些调整。
# Get the current IP of the AWS instance the script is launched from
LATEST_IP=$(wget -qO- http://checkip.amazonaws.com)
IP="${IP-$LATEST_IP}"
if [[ "${IP}" == "" ]]; then
echo "Could not find your public IP"
exit 1
fi
# Get the security group ID
GROUPID=$(aws ec2 describe-security-groups --query 'SecurityGroups[].[Tags[?Key==`<< parameters.tag-key >>`] | [0].Value, GroupId]' --output table | grep << parameters.tag-value >> | awk '{print $4}') [[ -n "${GROUPID}" ]] || (echo "Could not determine Security Group ID" && exit 0);
# Adding Rule SSH to Your Security Group
echo Allowing << parameters.description >> to access port $PORT from IP
$IP to the security group $GROUPID
aws ec2 authorize-security-group-ingress --group-id $GROUPID --ip-permissions '[{"IpProtocol": "tcp", "FromPort": '<< parameters.port >>', "ToPort": '<< parameters.port >>', "IpRanges": [{"CidrIp": "'$LATEST_IP/<< parameters.mask >>'", "Description": "'<< parameters.description >>'"}]}]'
# Closing the port
echo "Removing << parameters.description >> access from IP $IP to the security group $GROUPID"
# Delete IP rules matching port
aws ec2 revoke-security-group-ingress --group-id $GROUPID --ip-permissions '[{"IpProtocol": "tcp", "FromPort": '<< parameters.port >>', "ToPort": '<< parameters.port >>', "IpRanges": [{"CidrIp":"'$LATEST_IP/<< parameters.mask >>'", "Description": "'<< parameters.description >>'"}]}]'
【问题讨论】:
-
“我得到错误” - 你能澄清一下究竟是什么错误吗?
-
它抛出一个错误,说
|无法识别,所以我认为这是关于语法 -
什么是
parameters? -
老实说,我不知道。也许这就是问题所在。我想我需要在某处提供安全组的名称以及需要打开和关闭的端口,对吧?
标签: amazon-web-services aws-cli aws-security-group