-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocate.py
executable file
·77 lines (60 loc) · 1.8 KB
/
allocate.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
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
#!/usr/bin/python -u
import sys
from collections import Counter
import boto.ec2
def get_limit(ec2, domain):
limit = 0
temp_ips = []
try:
while True:
temp_ips.append(ec2.allocate_address(domain))
limit += 1
except:
if not limit:
print("Unable to allocate any IP addresses. Check your IAM credentials.")
finally:
for ip in temp_ips:
ip.release()
return limit
def allocate(ec2, domain, number, tolerance):
eips = []
subnets = Counter()
for x in range(number + tolerance):
if is_vpc:
ip = ec2.allocate_address(domain)
eips.append(ip)
subnets[ip.public_ip.split(".")[2]] += 1
retry = False
if subnets.most_common(1)[0][1] < number:
retry = True
for ip in eips:
ip.release()
return retry, subnets.most_common(1)[0][1]
if __name__ == "__main__":
try:
region = sys.argv[1]
number = int(sys.argv[2])
is_vpc = bool(sys.argv[3])
except:
print("Usage: ./allocate.py <region> <ip_count> <is_vpc>")
print("Example: ./allocate.py us-east-1 5 True")
sys.exit()
try:
ec2 = boto.ec2.connect_to_region(region)
except:
print("Unable to connect. Check your AWS credentials.")
sys.exit()
domain = "vpc" if is_vpc else None
limit = get_limit(ec2, domain)
if not limit:
sys.exit()
if number > limit:
print("AWS IP Limit not enough. Can only allocate %s more IP addresses" % limit)
sys.exit()
tolerance = limit - number
retry = True
while retry:
retry, succesful_amount = allocate(ec2, domain, number, tolerance)
print("."),
else:
print("Allocated %s IP addresses" % succesful_amount)