Created
September 29, 2017 16:54
-
-
Save equivalent/b065dac71316b815fa98fafa0684dc85 to your computer and use it in GitHub Desktop.
AWS EC2 security group add current IP and remove old IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
check AWS CLI form more details https://aws.amazon.com/cli/