Created
August 27, 2017 14:40
-
-
Save reisjr/b3e5993bc6f6be9fd8517a3c3fa888a9 to your computer and use it in GitHub Desktop.
Update a security group adding a rule for SSH from the current 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
#!/bin/bash | |
## Simple script to update the security group to allow ssh access from current IP. | |
## You can specify the Public DNS name of the instance that you want to access | |
## and it will update the first SG found. You can also setup a default SG and | |
## the script will update it and the DNS name is not informed. | |
SG="sg-xxxxxxxx" | |
REGION="us-east-1" | |
if [ "$#" -ne 1 ]; then | |
echo "Using default security group '$SG'" | |
echo "You can provide a full EC2 hostname to update a SG for this host. Ex: update_sg_ip.sh ec2-54-76-190-252.eu-west-1.compute.amazonaws.com" | |
else | |
REGION=`echo $1 | cut -d. -f2` | |
echo "Region identified: $REGION" | |
SG=`aws ec2 describe-instances --region $REGION --filters "Name=dns-name,Values=$1" --query "Reservations[0].Instances[0].NetworkInterfaces[0].Groups[0].GroupId"` | |
echo "SG found: ${SG//\"}" | |
fi | |
ip=`curl -s https://api.ipify.org` | |
echo "Your current IP: $ip" | |
aws ec2 authorize-security-group-ingress --protocol tcp --port 22 --cidr $ip/32 --group-id ${SG//\"} --region $REGION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment