[英]How could I add current pc IP into security group with aws cli
我想将当前工作的 PC 的 IP 添加到安全组中,
并为其启用所有流量。 每次我都应该使用 web 仪表板手动执行此操作。
我怎么能用 shell 脚本来做呢?
以下是我使用的最常见的 aws cli 命令。
但是我找不到如何在特定的安全组中添加ip
。
list_instances(){
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value,InstanceId,PublicIpAddress,PrivateIpAddress]' --output text
}
start_instance(){
aws ec2 start-instances --instance-ids $1
}
这是一个脚本,用于确定当前计算机的 IP 地址,然后使用AWS 命令行界面 (CLI)添加对端口 22 (SSH) 和 3389 (RDP) 的访问权限——这比添加对所有端口的访问权限要安全得多。
# Retrieve current IP address
IP=`curl -s http://whatismyip.akamai.com/`
# Authorize access on ports 22 and 3389
aws ec2 authorize-security-group-ingress --group-name "SG-NAME" --protocol tcp --port 22 --cidr $IP/32 --profile class --output text
aws ec2 authorize-security-group-ingress --group-name "SG-NAME" --protocol tcp --port 3389 --cidr $IP/32 --profile class --output text
它假设 AWS CLI 可以通过实例元数据(在 Amazon EC2 实例上)或通过aws configure
的本地凭证文件访问凭证。
PowerShell 版本基于John Rotenstein的回答:
# Set Security Group name
$SG_ID="sg-903004f8"
# Retrieve current IP address
$IP=Invoke-WebRequest -UseBasicParsing http://whatismyip.akamai.com/
# Authorize access on ports 22 (SSH) and 3389 (RDP)
aws ec2 authorize-security-group-ingress --group-id "$SG_ID" --protocol tcp --port 22 --cidr "$IP/32"
aws ec2 authorize-security-group-ingress --group-id "$SG_ID" --protocol tcp --port 3389 --cidr "$IP/32"
这是一个工具,您可以使用它并轻松管理将您的 IP 与 AWS 安全组中的端口一起列入白名单。 您可以使用aws_ipadd命令创建和更新 AWS 安全组规则。
https://github.com/piyushsonigra/aws_ipadd
$ aws_ipadd my_project_ssh
Your IP 12.10.1.14/32 and Port 22 is whitelisted successfully.
$ aws_ipadd my_project_ssh
Modifying existing rule...
Removing old whitelisted IP '12.10.1.14/32'.
Whitelisting new IP '131.4.10.16/32'.
Rule successfully updated!
我为此创建了一个小的 shell 脚本,以检查 ElasticBeanstalk 上的 tomcat 日志:
#!/bin/sh
#myIp = my current dynamic IP
myIp=`curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
mySG=SSHSecurityGroup
#add my IP to security group
aws ec2 authorize-security-group-ingress --group-id sg-02aXXXXX --protocol tcp --port 22 --cidr $myIp/32 --profile=prod
echo `date` added shh access to stroll-env security group
#aws ec2 revoke-security-group-ingress --group-id sg-02aXXXXX --protocol tcp --port 22 --cidr $myIp/32 --profile=prod
echo `date` done...
PS :--protocol tcp --port 22 --profile=prod 表示我正在向我的 prod(生产)实例添加 ssh 访问权限。
我在寻找 awscli 命令来更新安全组时看到了这篇文章。 我想我会分享我最近使用 Boto3 和 Requests 编写的这个 Python 脚本,它将使用您当前的 IP 地址更新指定的安全组。 希望这可以帮助某人。
此 PowerShell 脚本会在授权当前 IP 之前清理当前授权的 IP。
# Retrieve current IP address
$IP=Invoke-WebRequest -UseBasicParsing http://whatismyip.akamai.com/
$sg="sg-00001"
$old_file_path = 'old_ip.txt'
if(Test-Path -Path $old_file_path){
$OldIp=Get-Content -Path $old_file_path
aws ec2 revoke-security-group-ingress --group-id "$sg" --protocol tcp --port 8443 --cidr "$OldIp/32"
}
aws ec2 authorize-security-group-ingress --group-id "$sg" --protocol tcp --port 8443 --cidr "$IP/32"
Set-Content -Path $old_file_path -Value $IP
我从这个来源修改了这个:- https://gist.github.com/niraj-shah/567da4146fe2912bb6b4626bc71471ea
尽情享受吧!
#!/bin/bash
# Retrieve current IP address
ip=$(curl -s https://api.ipify.org);
old_ip=$(head -1 $HOME/ActualIp.txt)
profile="default"
ports=(22 3306);
group_ids=(sg-087891bcfb9caea8d sg-013e1366ba61b6548);
fixed_ips=();
if [ "$ip" == "$old_ip" ]; then
echo "nothing to do"
else
for port in ${ports[@]}
do
for group_id in ${group_ids[@]}
do
# Display security group name
echo -e "\033[34m\nModifying Group: ${group_id}\033[0m and port number: ${port}";
# Get existing IP rules for group
ips=$(aws ec2 --profile=$profile describe-security-groups --filters Name=ip-permission.to-port,Values=$port Name=ip-permission.from-port,Values=$port Name=ip-permission.protocol,Values=tcp --group-ids $group_id --output text --query 'SecurityGroups[*].{IP:IpPermissions[?ToPort==`'$port'`].IpRanges}')
ips=$(sed 's/IP//g' <<< $ips)
for ip in $ips
do
echo -e "\033[31mRemoving IP: $ip\033[0m"
# Delete IP rules matching port
aws ec2 revoke-security-group-ingress --profile=$profile --group-id $group_id --protocol tcp --port $port --cidr $ip
done
# Get current public IP address
myip=$(curl -s https://api.ipify.org);
echo -e "\033[32mSetting Current IP: ${myip}\033[0m"
# Add current IP as new rule
aws ec2 authorize-security-group-ingress --profile=$profile --protocol tcp --port $port --cidr ${myip}/32 --group-id $group_id
#Print curret IP to a file for the next cron
curl -s https://api.ipify.org > $HOME/ActualIp.txt
# Loop through fixed IPs
#for ip in $fixed_ips
#do
#echo -e "\033[32mSetting Fixed IP: ${ip}\033[0m"
# Add fixed IP rules
#/home/pi/.local/bin/aws ec2 authorize-security-group-ingress --profile=$profile --protocol tcp --port $port --cidr ${ip} --group-id $group_id
#done
done
done
fi
这个小的 bash 脚本具有相同的目的,端口号可以是 cli 中的位置参数 -
#!/bin/bash
TargetAddr='<IP>'
aws_sg_id='<sg-id>'
myip=$(curl -s ifconfig.me)
echo -e "Updating IP $myip in Security Group: $aws_sg_id"
aws --profile <profile-name> ec2 authorize-security-group-ingress --group-id $aws_sg_id --protocol tcp --port 22 --cidr $myip/32 2>/dev/null
#Verifying the Access
/usr/bin/nc -zv -w1 $TargetAddr 22
这是我的 bash 脚本,允许您终端的公共 IP 地址到入站规则中的端口 5432(Postgres SQL)。
假设您的安全组不在默认 VPC 中,因此您不能使用--group-name
,但您只知道您的安全组名称。
--group-id (string)
The ID of the security group. You must specify either the security group ID or the security group name in the request. For security groups in a nondefault VPC, you must specify the security group ID.
--group-name (string)
[EC2-Classic, default VPC] The name of the security group. You must specify either the security group ID or the security group name in the request. For security groups in a nondefault VPC, you must specify the security group ID.
然后你可以这样做
#!/bin/bash
sg_id=$(aws ec2 describe-security-groups --filters Name=group-name,Values=your_security_group_name --query "SecurityGroups[*].[GroupId]" --output text)
public_ip=$(curl -s ifconfig.me)
echo "your public ip address is: $public_ip"
aws ec2 authorize-security-group-ingress \
--group-id $sg_id \
--protocol tcp \
--port 5432 \
--cidr $public_ip/24
echo "your public ip address have been added to the RDS sg inbound rule!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.