Created
May 22, 2017 15:54
-
-
Save mattrasband/839b44e48221ed1d0b75a67e5e953f72 to your computer and use it in GitHub Desktop.
Add a public IP to an AWS security group and schedule automatic removal
This file contains hidden or 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
#!/usr/bin/env bash | |
# You need to set some vars somewhere: | |
# SECURTITY_GROUP=<your_security_group_id> | |
# on mac you need to enable running the `at` command: | |
# $ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist | |
current_ip=$(curl -s http://checkip.amazonaws.com/) | |
echo "Current IP: ${current_ip}" | |
current_ip="${current_ip}/32" | |
for existing_ip in $(aws ec2 describe-security-groups --group-ids $SECURITY_GROUP | jq -r '.SecurityGroups[0].IpPermissions[1].IpRanges[] | .CidrIp'); do | |
if [[ $existing_ip == "$current_ip" ]]; then | |
echo "IP already registered, bailing" | |
exit | |
fi | |
done | |
echo "What location are you at?" | |
read location_description | |
echo "IP: $current_ip; LOCATION: $location_description" >> ~/.aws_added_ips | |
echo "IP doesn't exist, adding as ${current_ip}" | |
aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP --protocol tcp --port 22 --cidr $current_ip | |
echo "Scheduling removal at 5:00pm" | |
at 5:00 PM <<COMMAND | |
aws ec2 revoke-security-group-ingress --group-id $SECURITY_GROUP --protocol tcp --port 22 --cidr $current_ip | |
COMMAND |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment