Skip to content

Instantly share code, notes, and snippets.

@equivalent
Created September 29, 2017 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equivalent/b065dac71316b815fa98fafa0684dc85 to your computer and use it in GitHub Desktop.
Save equivalent/b065dac71316b815fa98fafa0684dc85 to your computer and use it in GitHub Desktop.
AWS EC2 security group add current IP and remove old IP
myip=$(dig +short myip.opendns.com @resolver1.opendns.com) # this will fetch my current IP
#myip=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
mkdir -p "./tmp"
touch "./tmp/old_ip.txt"
removing_ip=$(cat ./tmp/old_ip.txt)
adding_ip=$myip
echo $adding_ip > ./tmp/old_ip.txt
if [ -z ${removing_ip+not_set} ]; then
echo "Removing nothing"
else
echo "removing $removing_ip from my ssh Development Security Grop Connection"
aws ec2 --profile default revoke-security-group-ingress \
--group-name my-security-group-name \
--protocol tcp \
--port 22 \
--cidr $removing_ip/32
echo "removing $removing_ip from my https Development Security Grop Connection"
aws ec2 --profile default revoke-security-group-ingress \
--group-name my-security-group-name \
--protocol tcp \
--port 443 \
--cidr $removing_ip/32
fi
if [ -z ${adding_ip+not_set} ]; then
echo "Adding nothing"
else
echo "adding $adding_ip from my ssh Development Security Grop Connection"
aws ec2 --profile default authorize-security-group-ingress \
--group-name my-security-group-name \
--protocol tcp \
--port 22 \
--cidr $adding_ip/32
echo "removing $adding_ip from my https Development Security Grop Connection"
aws ec2 --profile default authorize-security-group-ingress \
--group-name my-security-group-name \
--protocol tcp \
--port 443 \
--cidr $adding_ip/32
fi
echo 'Finished'
@equivalent
Copy link
Author

check AWS CLI form more details https://aws.amazon.com/cli/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment