Python 3: create a list of possible ip addresses from a CIDR notation

后端 未结 7 889
花落未央
花落未央 2020-12-08 07:57

I have been handed the task of creating a function in python (3.1) that will take a CIDR notation and return the list of possible ip addresses. I have looked around python.

7条回答
  •  攒了一身酷
    2020-12-08 08:20

    In Python 3 as simple as

    >>> import ipaddress
    >>> [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')]
    ['192.0.2.0', '192.0.2.1', '192.0.2.2',
    '192.0.2.3', '192.0.2.4', '192.0.2.5',
    '192.0.2.6', '192.0.2.7', '192.0.2.8',
    '192.0.2.9', '192.0.2.10', '192.0.2.11',
    '192.0.2.12', '192.0.2.13', '192.0.2.14',
    '192.0.2.15']
    

提交回复
热议问题