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


在这篇文章中,我们将学习如何从CIDR地址生成IP地址。

使用的方法

以下是完成此任务的各种方法:

  • 生成IPv4网络地址

  • 生成IPv6网络地址

  • 访问CIDR地址的IP地址

方法1:IPv4Network

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 使用`import`关键字导入`ipaddress`模块。

  • 使用`ipaddress`模块的`ip_network()`函数(返回地址的网络类型)从输入的CIDR地址(IPv4Network)获取IP地址。

  • 使用`for`循环遍历上述IPv4地址列表。

  • 打印列表中当前的IPv4地址。

示例

以下程序从给定的CIDR地址返回IPv4地址列表:

Open Compiler
# 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地址列表:

Open Compiler
# 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

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

方法3:访问CIDR地址的IP地址

我们可以通过将所有对应的IP地址作为列表获取,并使用`[]`运算符(即索引方法)访问列表元素来访问给定CIDR地址的IP地址。

示例

以下程序演示如何访问CIDR地址的IP地址:

Open Compiler
# 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+ 浏览量

开启您的职业生涯

完成课程后获得认证

开始学习
广告