Skip to content

Instantly share code, notes, and snippets.

@twexler
Created December 22, 2014 23:09
Show Gist options
  • Save twexler/18c876aa442fb878d111 to your computer and use it in GitHub Desktop.
Save twexler/18c876aa442fb878d111 to your computer and use it in GitHub Desktop.
ec2 subnet address allocation script
import boto.ec2
import boto.vpc
def subnet_stats(subnet_id):
addresses = []
ints = ec2.get_all_network_interfaces(filters={'subnet-id': subnet_id})
for i in ints:
addresses.append(i.private_ip_address)
for addr in i.private_ip_addresses:
if addr.private_ip_address != i.private_ip_address:
addresses.append(addr.private_ip_address)
addresses.sort(lambda x, y: 1 if int(x.split('.')[-1]) > int(y.split('.')[-1]) else -1)
print "\tNumber of allocated addresses: %d" % len(addresses)
print "\tAddresses left: %d" % (254 - int(len(addresses)))
print "\tAddresses: %s\n" % addresses
ec2 = boto.ec2.connect_to_region('us-west-1')
vpc = boto.vpc.connect_to_region('us-west-1')
subnets = vpc.get_all_subnets()
for subnet in subnets:
print "Stats for subnet \"%s\" (%s):" % (subnet.tags['Name'], subnet.id)
print "\tAZ: %s" % subnet.availability_zone
print "\tCIDR: %s" % subnet.cidr_block
subnet_stats(subnet.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment