Skip to content

Instantly share code, notes, and snippets.

@eladkehat
Created July 20, 2016 06:58
Show Gist options
  • Save eladkehat/5073a25fa3848c0821193ed4b819cd59 to your computer and use it in GitHub Desktop.
Save eladkehat/5073a25fa3848c0821193ed4b819cd59 to your computer and use it in GitHub Desktop.
This script adds your current IP address to the security group named SSH on AWS. Useful for the security conscious among us who want to limit SSH access to their EC2 instances by IP address but don't want to get into the AWS console any time their home IP changes.
#!/usr/bin/env bash
# Requirements:
# * The AWS CLI should be installed: https://aws.amazon.com/cli/
# * A credentials file has the correct account credentials configured as default (see the CLI reference)
# * There exists a security group in the default region whose name is SSH
MYIP=$(curl -s http://checkip.amazonaws.com/)
#echo "Current IP is: $MYIP"
KNOWN=$(aws ec2 describe-security-groups | grep -c "$MYIP")
if [ $KNOWN -eq 0 ]; then
aws ec2 authorize-security-group-ingress --group-name SSH --protocol tcp --port 22 --cidr "$MYIP"/32;
echo "Added $MYIP to the security group SSH.";
else
echo "IP $MYIP already set. No changes were made to the security group.";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment