7

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 provide the code in python to calculate the subnet mask? Preferably I could get the result as 32-bit integer so that it is easy to hash and then reinterpret as dotted quad when necessary for printing. I see that python has a socket library which is basically a wrapper around the unix socket api. I also saw it has the function inet_ntoa(), but it returns some sort of packet struct. I'm not terribly familiar with the Python struct library, so I was hoping some other would have some ideas. Thanks!

smci
  • 32,567
  • 20
  • 113
  • 146
themaestro
  • 13,750
  • 20
  • 56
  • 75

2 Answers2

17

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

For Python2

>>> # Use ipaddr in python 2.x
>>> import ipaddr
>>> mask = ipaddr.IPv4Network('192.192.45.1/25')
>>> mask.netmask
IPv4Address('255.255.255.128')
>>>

for Python 3...

>>> # Use ipaddress in python 3.x
>>> import ipaddress
>>> mask = ipaddress.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>
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • 1
    @themaestro, I am glad it helps. Check out [ipaddr's wiki](http://code.google.com/p/ipaddr-py/wiki/Using3144) – Mike Pennington May 09 '12 at 02:22
  • 1
    In python 3.x the same can be accomplished with the built-in version of this module, now called "ipaddress" (i.e., import ipaddress ...). – JJC Nov 09 '15 at 17:52
8

You can calcuate the 32 bit value of the mask like this

(1<<32) - (1<<32>>mask_length)

eg.

>>> import socket, struct
>>> mask_length = 24
>>> mask = (1<<32) - (1<<32>>mask_length)
>>> socket.inet_ntoa(struct.pack(">L", mask))
'255.255.255.0'
John La Rooy
  • 295,403
  • 53
  • 369
  • 502