Created
May 25, 2012 07:50
-
-
Save amites/2786470 to your computer and use it in GitHub Desktop.
python add current IP address to ec2
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/python | |
import urllib2 | |
from boto.ec2.connection import EC2Connection | |
try: | |
from django.conf import settings | |
except ImportError: | |
class settings: | |
AWS_ACCESS_KEY_ID = 'YOUR_ACCESS_KEY' | |
AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_ACCESS_KEY' | |
def findIP(): | |
''' | |
simple python script to return your public IP address using whatismyip.com | |
if executed from command line will display IP | |
Returns your IP address. Includes setting the header to match the request | |
see http://www.whatismyip.com/faq/automation.asp | |
''' | |
headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0)' \ | |
+ ' Gecko/20100101 Firefox/12.0' } | |
return = urllib2.urlopen( | |
urllib2.Request( | |
"http://automation.whatismyip.com/n09230945.asp", | |
None, headers ) | |
).read() | |
def addIPSecurity(port=22, group=0): | |
conn = EC2Connection( | |
settings.AWS_ACCESS_KEY_ID, | |
settings.AWS_SECRET_ACCESS_KEY) | |
conn.get_all_security_groups() | |
rs = conn.get_all_security_groups() | |
sg = rs[group] | |
pub_ip = findIP() | |
pub_ip_range = '%s0/24' % pub_ip[:-len(pub_ip.split('.')[-1])] | |
return sg.authorize(ip_protocol='tcp', from_port=port, to_port=port, | |
cidr_ip=pub_ip_range) | |
if __name__=="__main__": | |
addIPSecurity() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment