Get Ip Mask From Ip Address And Mask Length In Python
Given an IP Address in dotted quad notation, for example: 192.192.45.1 And a mask length for example 8, 16, 24 typically, but could be anything i.e. 17. Can somebody please provi
Solution 1:
The simplest way is to use google's ipaddr module. I assume a 25 bit mask below, but as you say, it could be anything
>>>import ipaddr>>>mask = ipaddr.IPv4Network('192.192.45.1/25')>>>mask.netmask
IPv4Address('255.255.255.128')
>>>
The module is rather efficient at manipulating IPv4 and IPv6 addresses... a sample of some other functionality in it...
>>>## Subnet number?>>>mask.network
IPv4Address('192.192.45.0')
>>>>>>## RFC 1918 space?>>>mask.is_private
False
>>>
>> ## The subnet broadcast address
>>>mask.broadcast
IPv4Address('192.192.45.127')
>>>mask.iterhosts()
<generator object iterhosts at 0xb72b3f2c>
Post a Comment for "Get Ip Mask From Ip Address And Mask Length In Python"