【问题标题】:how to parse/extract IP from IPrange/CIDR using python? [closed]如何使用 python 从 IPrange/CIDR 解析/提取 IP? [关闭]
【发布时间】:2023-03-28 01:15:02
【问题描述】:

我想使用 list.txt 从批量 IPrange 或 CIDR 中解析/提取 IP

喜欢iprange:

1.0.0.0-1.255.255.255

2.0.0.0-2.255.255.255

3.0.0.0-3.255.255.255

或 CIDR:

1.0.16.0/20

1.0.64.0/18

1.1.64.0/18

到 IP

作为 output.txt :

1.10.40.10

10.10.40.10

3.10.40.10

【问题讨论】:

    标签: python python-3.x python-2.7


    【解决方案1】:

    我认为你只想要 Python 3 附带的 ipaddress 包。

    https://docs.python.org/3/library/ipaddress.html

    那么你可以做例如:

    import ipaddress
    net = ipaddress.ip_network('1.1.64.0/18').hosts()
    for ip in net:
       print(ip)
    

    它有另一个方法,ipaddress.summarize_address_range,应该覆盖另一半。

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2019-03-28
      • 2020-09-20
      相关资源
      最近更新 更多