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 } " )有關以開發人員為導向的信息,請參見貢獻。