aws-cidr-finder是一种Python CLI工具和库,在AWS VPC中找到未使用的CIDR块(IPv4或IPv6)。这是一个非常简单的工具,但是对于管理一个或多个VPC的许多子网并且不想花费使用AWS IPAM等解决方案所需的资金的用户,它可能有很大的帮助。
通过一个示例,最容易看到此工具的价值。想象一下,我们在AWS中具有以下设置:
172.31.0.0/16的VPC,带有Hello World的Name标签172.31.0.0/20172.31.16.0/20172.31.32.0/20172.31.48.0/20172.31.64.0/20172.31.80.0/20myprofile aws-cidr-finder可让您快速计算VPC中仍然可用的CIDR,而不必做很多令人讨厌/乏味的八位位组数学。如果我们发出此命令:
aws-cidr-finder --profile myprofile我们应该看到此输出:
Here are the available CIDR blocks in the 'Hello World' VPC (VPC CIDR block '172.31.0.0/16'):
CIDR IP Count
--------------- ----------
172.31.96.0/19 8192
172.31.128.0/17 32768
Total 40960
您应该注意到,默认情况下, aws-cidr-finder将通过合并相邻的免费CIDR块自动“简化” CIDR,以便所得的表显示每个CIDR的最大连续空间(换句话说,结果表的行可能较少,可能的行数量最少)。这就是为什么命令的结果仅显示两个CIDR:A /19和A /17 。
请注意,第一个CIDR是
/19而不是,例如/18,因为/18CIDR必须从IP地址172.31.64.0开始,并且该IP地址已经由子网采用!
但是,我们可以通过指定--prefix cli标志来更改此“简化”行为:
aws-cidr-finder --profile myprofile --prefix 20现在,预期的输出应该看起来像这样:
Here are the available CIDR blocks in the 'Hello World' VPC (VPC CIDR block '172.31.0.0/16'):
CIDR IP Count
--------------- ----------
172.31.96.0/20 4096
172.31.112.0/20 4096
172.31.128.0/20 4096
172.31.144.0/20 4096
172.31.160.0/20 4096
172.31.176.0/20 4096
172.31.192.0/20 4096
172.31.208.0/20 4096
172.31.224.0/20 4096
172.31.240.0/20 4096
Total 40960
通过--prefix参数,我们现在可以将可用的网络空间查询到所需的细节级别。请注意,如果我们指定一个值--prefix值低于可用CIDRS列表中的任何前缀,则将跳过这些CIDR。例如,如果我们运行以下内容:
aws-cidr-finder --profile myprofile --prefix 18我们应该看到此输出:
Note: skipping CIDR '172.31.96.0/19' because its prefix (19) is numerically greater than the requested prefix (18)
Here are the available CIDR blocks in the 'Hello World' VPC (VPC CIDR block '172.31.0.0/16'):
CIDR IP Count
--------------- ----------
172.31.128.0/18 16384
172.31.192.0/18 16384
Total 32768
跳过的CIDR是172.31.96.0/19 CIDR,因为不可能将A /19 CIDR转换为一个或多个/18 CIDR。
如果您安装了Python> = 3.10和<4.0,则可以使用此类内容从PYPI安装aws-cidr-finder :
pip install aws-cidr-finder为了使用此CLI,所有需要配置的都是AWS CLI配置文件或键盘。前者可以使用CLI上的--profile参数指定,而KeApair必须在环境变量中指定。如果两者同时可用, aws-cidr-finder会更喜欢配置文件。
Keypair方法的环境变量为AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY ,以及可选的AWS_SESSION_TOKEN (如果使用会话进行认证)。这些是BOTO使用的环境变量。
您还应确保您使用的个人资料/键盘具有通过BOTO进行基础API呼叫所需的AWS IAM访问权限。这是一个最小的IAM政策文件,填充了此要求:
{
"Effect" : " Allow " ,
"Action" : [
" ec2:DescribeVpcs " ,
" ec2:DescribeSubnets "
],
"Resource" : " * "
}阅读有关上面显示的动作的更多信息。
有关此工具的CLI接口的详细演示,请参见上面的示例。您也可以使用aws-cidr-finder --help查看命令行选项。
设置:
from aws_cidr_finder import JSONOutput , find_available_cidrs
# All arguments
output : JSONOutput = find_available_cidrs ( profile_name = "" , region = "" , ipv6 = False , desired_prefix = 20 )
# Minimal arguments (profile-based authentication)
output : JSONOutput = find_available_cidrs ( profile_name = "" )
# Minimal arguments (environment variable-based authentication)
output : JSONOutput = find_available_cidrs ()
# Other miscellaneous combinations
output : JSONOutput = find_available_cidrs ( profile_name = "" , ipv6 = True )
output : JSONOutput = find_available_cidrs ( profile_name = "" , desired_prefix = 16 )
output : JSONOutput = find_available_cidrs ( region = "" )
# ...and so on访问CIDR数据:
output : JSONOutput = find_available_cidrs (...) # See above
for message in output [ "messages" ]:
# Print the messages that would have been written to STDOUT when using the CLI
print ( message )
for cidr in output [ "cidrs_not_converted_to_prefix" ]:
# If aws-cidr-finder could not convert a given available CIDR block into one or more CIDR blocks
# with the requested desired_prefix, it will be returned in this list
# Note: this list is only populated if you passed desired_prefix to find_available_cidrs
print ( f"aws-cidr-finder did not convert the following CIDR block to the desired prefix: { cidr } " )
for vpc in output [ "data" ]:
# Print all the information that is available in the VPC dict
print ( f'VPC ID: { vpc [ "id" ] } ' )
print ( f'VPC Name: { vpc [ "name" ] } ' )
print ( f'VPC CIDR: { vpc [ "cidr" ] } ' )
for cidr in vpc [ "available_cidr_blocks" ]:
print ( f"Available CIDR block: { cidr } " )有关以开发人员为导向的信息,请参见贡献。