-
Notifications
You must be signed in to change notification settings - Fork 2
/
ip-assoc.py
32 lines (26 loc) · 1.27 KB
/
ip-assoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import boto
import boto.ec2
import ConfigParser
import os.path
import sys
import datetime
from optparse import OptionParser
check = os.path.isfile(os.path.expanduser('~/.aws.conf'))
if cmp(check,False) == 0:
print "~/.aws.conf is missing"
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.aws.conf'))
id = config.get("AWS", "consumer_key", raw=True)
key = config.get("AWS", "consumer_secret", raw=True)
parser = OptionParser()
parser.add_option("-i", "--instance", dest="instance", help="Instance id of the instance.", metavar="INSTANCE_ID")
parser.add_option("-a", "--associate", dest="assoc", help="IP address to associate.", metavar="IP_ADDR")
parser.add_option("-d", "--disassociate", dest="disassoc", help="IP address to disassociate.", metavar="IP_ADDR")
parser.add_option("-r", "--region", dest="region", help="Region to be connected", metavar="REGION_NAME")
(options, args) = parser.parse_args()
region_obj = boto.ec2.get_region(options.region,aws_access_key_id=id,aws_secret_access_key=key)
conn = boto.connect_ec2(aws_access_key_id=id,aws_secret_access_key=key,region=region_obj)
if options.assoc:
conn.associate_address(instance_id=options.instance, public_ip=options.assoc)
elif options.disassoc:
conn.disassociate_address(public_ip=options.disassoc)