Python: list of IP addresses

What is the most Pythonic way to create a list of IP addresses based on the IPad network netaddr IPRange or netaddr.

If I use them, it includes subnets and broadcast addresses:

hosts = list(IPRange('212.55.64.0','212.55.127.255'))
hosts = IPNetwork('192.168.0.1/24')

So what I need for IPNetwork (192.168.0.0/27) is a list from 192.168.0.1 to 192.168.0.31, but you should not include 192.168.0.0 and 192.168.0.32.

EDIT

Thanks for the info on how to do this using IPy. Does anyone know if this can be done using netaddr?

+3
source share
3 answers

After a little research, I found this method:

 l = list(netaddr.IPNetwork('192.168.0.0/27').iter_hosts())
0
source

script, , , netaddr
(Python 2.7, linux)

from netaddr import *

def addr(address, prefix):
    ip = IPNetwork(address)
    ip.prefixlen = int(prefix)
    return ip

myip = addr('192.168.0.0', '27')

for usable in myip.iter_hosts():
    print '%s' % usable
+1

Source: https://habr.com/ru/post/1781973/


All Articles