-
Notifications
You must be signed in to change notification settings - Fork 12
/
elastic-ip-check
123 lines (97 loc) · 4.31 KB
/
elastic-ip-check
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# Copyright 2012, 42Lines, Inc.
# Original Author: Jim Browne
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import boto
from optparse import OptionParser
VERSION = "1.0"
usage = """%prog [options]
Check for unused elastic IPs in all EC2 regions. Optionally report on
in-use elastic IPs.
"""
def elastic_ip_compare(options, accounts=None):
"""
Will check for unallocated elastic IPs in all EC2 regions for all
of the accounts supplied.
:type accounts: dict
:param accounts: A dictionary contain account information. The key is
a string identifying the account (e.g. "dev") and the
value is a tuple or list containing the access key
and secret key, in that order.
If this value is None, the credentials in the boto
config will be used.
"""
if not accounts:
creds = (boto.config.get('Credentials', 'aws_access_key_id'),
boto.config.get('Credentials', 'aws_secret_access_key'))
accounts = {'main': creds}
for account_name in accounts:
ufmt = "Account {0} Region {1} unused elastic IPs:"
ifmt = "Account {0} Region {1} in-use elastic IPs:"
if options.trace:
print "Scanning account {0}".format(account_name)
ak, sk = accounts[account_name]
for region in boto.ec2.regions():
if options.trace:
print " Scanning region {0}".format(region.name)
uheader = ufmt.format(account_name, region.name)
iheader = ifmt.format(account_name, region.name)
conn = region.connect(aws_access_key_id=ak,
aws_secret_access_key=sk)
elasticips = conn.get_all_addresses()
unused = [ip.public_ip for ip in elasticips if not ip.instance_id]
if unused:
if not options.quiet:
print uheader
print '\n'.join(unused)
inuse = [ip for ip in elasticips if ip.instance_id]
if inuse and options.used:
if not options.quiet:
print iheader
for ip in inuse:
output = "{0} Instance {1}".format(ip.public_ip,
ip.instance_id)
if options.itag:
value = ''
iids = [ip.instance_id]
resvs = conn.get_all_instances(instance_ids=iids)
for r in resvs:
for i in r.instances:
if i.id == ip.instance_id:
value = i.tags.get(options.itag, '')
break
if value:
output += " ({0:.32})".format(value)
print output
if __name__ == '__main__':
parser = OptionParser(version=VERSION, usage=usage)
parser.add_option("--quiet",
help="Only print IPs, no headers",
action="store_true", dest="quiet")
parser.add_option("--used",
help="Print in-use IPs as well",
action="store_true", dest="used")
parser.add_option("--itag",
help="Print specified tag for each instance",
dest="itag")
parser.add_option("--debug",
help="Emit copious information to aid script debugging",
action="store_true", dest="debug")
parser.add_option("--trace",
help="Trace execution steps",
action="store_true", dest="trace")
(options, args) = parser.parse_args()
if options.debug:
options.trace = 1
elastic_ip_compare(options)