如何使用Python從CIDR地址生成IP地址?


在這篇文章中,我們將學習如何從CIDR地址生成IP地址。

使用的方法

以下是完成此任務的各種方法:

  • 生成IPv4網路地址

  • 生成IPv6網路地址

  • 訪問CIDR地址的IP地址

方法1:IPv4Network

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用`import`關鍵字匯入`ipaddress`模組。

  • 使用`ipaddress`模組的`ip_network()`函式(返回地址的網路型別)從輸入的CIDR地址(IPv4Network)獲取IP地址。

  • 使用`for`迴圈遍歷上述IPv4地址列表。

  • 列印列表中當前的IPv4地址。

示例

以下程式從給定的CIDR地址返回IPv4地址列表:

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv4 network address)
netIpv4Address = ipaddress.ip_network('123.45.66.64/27')
print("The Following are the IPv4 Addresses in the given CIDR Address(123.45.66.64/27)")
# traversing through the above IPv4 addresses list
for i in netIpv4Address:
   # printing the current IPv4 address
   print(i)

輸出

執行上述程式後,將生成以下輸出:

The Following are the IPv4 Addresses in the given CIDR Address(123.45.66.64/27)
123.45.66.64
123.45.66.65
123.45.66.66
123.45.66.67
123.45.66.68
123.45.66.69
123.45.66.70
123.45.66.71
123.45.66.72
123.45.66.73
123.45.66.74
123.45.66.75
123.45.66.76
123.45.66.77
123.45.66.78
123.45.66.79
123.45.66.80
123.45.66.81
123.45.66.82
123.45.66.83
123.45.66.84
123.45.66.85
123.45.66.86
123.45.66.87
123.45.66.88
123.45.66.89
123.45.66.90
123.45.66.91
123.45.66.92
123.45.66.93
123.45.66.94
123.45.66.95

方法2:IPv6Network

將IPv6網路的CIDR地址作為引數傳遞給`ip_network()`函式,並透過遍歷結果列印其所有IPv6網路。

示例

以下程式從給定的CIDR地址返回IPv6地址列表:

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv6 network address)
netIpv6Address = ipaddress.ip_network('12:3456:78:90ab:cd:ef11:23:30/125')
print("The Following are the IPv6 Addresses in the given CIDR Address(12:3456:78:90ab:cd:ef11:23:30/125)")
# traversing in the above Ipv6Address
for i in netIpv6Address:
   # printing the current Ipv6Address
   print(i)

輸出

執行上述程式後,將生成以下輸出:

The Following are the IPv6 Addresses in the given CIDR Address(12:3456:78:90ab:cd:ef11:23:30/125)
12:3456:78:90ab:cd:ef11:23:30
12:3456:78:90ab:cd:ef11:23:31
12:3456:78:90ab:cd:ef11:23:32
12:3456:78:90ab:cd:ef11:23:33
12:3456:78:90ab:cd:ef11:23:34
12:3456:78:90ab:cd:ef11:23:35
12:3456:78:90ab:cd:ef11:23:36
12:3456:78:90ab:cd:ef11:23:37

方法3:訪問CIDR地址的IP地址

我們可以透過將所有對應的IP地址作為列表獲取,並使用`[]`運算子(即索引方法)訪問列表元素來訪問給定CIDR地址的IP地址。

示例

以下程式演示如何訪問CIDR地址的IP地址:

# importing ipaddress module
import ipaddress
# getting the IP addresses from an input CIDR address(IPv4 network address)
netIpv4Address = ipaddress.ip_network('123.45.66.64/27')
# accessing the first Ipv4Address from the resultant list
print("First Ipv4Address from the list:", netIpv4Address[0])
 
# accessing the second Ipv4Address from the resultant list
print("Second Ipv4Address from the list:", netIpv4Address[1])
 
# accessing the last Ipv4Address from the resultant list
print("Last Ipv4Address from the list:", netIpv4Address[-1])

輸出

執行上述程式後,將生成以下輸出:

First Ipv4Address from the list: 123.45.66.64
Second Ipv4Address from the list: 123.45.66.65
Last Ipv4Address from the list: 123.45.66.95

結論

本文教我們如何從提供的CIDR地址中提取每個IP地址。我們使用了兩種方法,一種用於獲取所有IPv4地址,另一種用於收集IPv6地址。我們還展示瞭如何在將IP地址轉換為列表後使用`[]`運算子訪問它們。

更新於:2023年1月24日

4K+ 瀏覽量

開啟您的職業生涯

完成課程後獲得認證

開始學習
廣告