Created
July 2, 2019 01:18
-
-
Save rezamt/a4ea23451412f35e3f559c2b2d5a814d to your computer and use it in GitHub Desktop.
This file contains 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
import argparse | |
import sys | |
import boto3 | |
from netaddr import IPNetwork | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--vpc-id', required=True) | |
parser.add_argument('--region', default='us-east-1') | |
args = parser.parse_args() | |
client = boto3.client('ec2', region_name=args.region) | |
subnets = client.describe_subnets( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [ | |
args.vpc_id | |
] | |
} | |
] | |
) | |
vpc_subnets = subnets['Subnets'] | |
if len(vpc_subnets) == 0: | |
sys.exit("Could not find any subnets for VPC {}".format(args.vpc_id)) | |
for vpc_subnet in vpc_subnets: | |
try: | |
subnet_name = [ | |
t for t in vpc_subnet['Tags'] if t['Key'] == 'Name'][0]['Value'] | |
except IndexError: | |
subnet_name = "<none>" | |
print "\tChoice: {index} -- {subnet_name} - {subnet_id}".format( | |
index=vpc_subnets.index(vpc_subnet), | |
subnet_name=subnet_name, | |
subnet_id=vpc_subnet['SubnetId']) | |
try: | |
choice_input = raw_input | |
except NameError: | |
choice_input = input | |
choice = choice_input('Choose a subnet (a for all):') | |
try: | |
choice_number = int(choice) | |
if (choice_number < 0) or ((choice_number + 1) > len(vpc_subnets)): | |
sys.exit("{} not a vaild choice".format(choice)) | |
vpc_subnets = [vpc_subnets[choice_number]] | |
except ValueError: | |
if choice == "a": | |
pass | |
else: | |
sys.exit("{} not a vaild choice".format(choice)) | |
print "\n" | |
for vpc_subnet in vpc_subnets: | |
try: | |
subnet_name = [ | |
t for t in vpc_subnet['Tags'] if t['Key'] == 'Name'][0]['Value'] | |
except IndexError: | |
subnet_name = "<none>" | |
# this list consists of IPAddress objects | |
addresses = list(IPNetwork(vpc_subnet['CidrBlock'])) | |
# clean up first 4 and last 1 (reserved) | |
del addresses[0:4] | |
del addresses[-1] | |
# create a list of strings containing ip addresses | |
raw_addresses = [str(i) for i in addresses] | |
eni_response = client.describe_network_interfaces( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [ | |
args.vpc_id, | |
] | |
}, | |
{ | |
'Name': 'subnet-id', | |
'Values': [ | |
vpc_subnet['SubnetId'] | |
] | |
}, | |
] | |
) | |
enis = eni_response['NetworkInterfaces'] | |
for eni in enis: | |
for ip in eni['PrivateIpAddresses']: | |
if ip['PrivateIpAddress'] in raw_addresses: | |
raw_addresses.remove(ip['PrivateIpAddress']) | |
print "\n-----" | |
print "Subnet Name: {} - Subnet ID: {} - AZ: {}".format( | |
subnet_name, | |
vpc_subnet['SubnetId'], | |
vpc_subnet['AvailabilityZone'] | |
) | |
print "Available Addresses Count: {}".format( | |
vpc_subnet['AvailableIpAddressCount']) | |
print "Available Addresses:\n{}".format(', '.join(raw_addresses)) | |
print "-----\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment