python - Calculate the N IP of a network -
this question has answer here:
i know n ip of given network ip is.
for example 192.168.0.3 3 ip of network 192.168.0.0/20. 256 ip 192.168.1.0
is there way calculate fast in python? know ipcalc not has option this.
in ipaddress
module in standard library, network object can used iterable of addresses. so, normal find in sequence:
>>> import ipaddress >>> addr = ipaddress.ip_address('192.168.0.1') >>> net = ipaddress.ip_network('192.168.0.0/20') >>> net[256] ipv4address('192.168.1.0') >>> next(i i, in enumerate(net) if == addr) 3
see howto more explanation.
note works fine addresses in integer form dotted strings, ipv6 ipv4, etc.
if you're using python 2.x, you'll need backport on pypi. if you're using 3.0-3.2, believe backport doesn't yet support those, in case best bet use library based on, ipaddr
instead.
Comments
Post a Comment