Quick way to calculate subnet IP and broadcast IP

6,071

Solution 1

There are various websites and applications that will do the calculation for you, such as http://www.subnet-calculator.com/cidr.php.

Otherwise, use the netmask to calculate the size of the subnet (2 to the power of the number of zero bits in the netmask). Find the highest IP address that's an exact multiple of the subnet size but is lower than the router's IP address. That IP address is the network address, and the IP address plus the subnet size minus 1 is the broadcast address.

Solution 2

If something takes time, script it.

Python has the ipaddress module:

$ python
Python 3.4.2 (default, Oct  8 2014, 13:44:52) 
>>> import ipaddress
>>> x = ipaddress.IPv6Interface("2001:470:709a:107::1a2b:3c/64")
>>> x.network
IPv6Network('2001:470:709a:107::/64')
>>> y = ipaddress.IPv4Interface("194.219.181.195/26")
>>> y.network
IPv4Network('194.219.181.192/26')
>>> y.network.network_address
IPv4Address('194.219.181.192')
>>> y.network.broadcast_address
IPv4Address('194.219.181.255')

Which would result in:

#!/usr/bin/env python
import ipaddress
import sys

for arg in sys.argv[1:]:
    addr = ipaddress.ip_interface(arg)
    print("address =", addr)
    print("network =", addr.network)
    if addr.version == 4:
        print("netmask =", addr.netmask)
        print("broadcast =", addr.network.broadcast_address)
    print()
Share:
6,071

Related videos on Youtube

f855a864
Author by

f855a864

Updated on September 18, 2022

Comments

  • f855a864
    f855a864 over 1 year

    I'm having trouble findind a quick/effective method to determine valid IP range based on given router IP and netmask

    I know how to calculate the subnet IP and the broadcast IP by converting all the IP and netmask to binary and do bitwise AND / OR but that approach takes a lot time and will easily lead to blunders ...

  • f855a864
    f855a864 over 9 years
    Thanks, but actually I'm not allowed to use any tool, in exams I have to calculate them my self, and I tend to make blunders in calculations so ...
  • valer
    valer almost 6 years
    Is this a broadcast address: 87.35.15.7/29? if i user your trick. then i get the network address:80.32.8.0, and the broadcast address 87.39.15.7 so that means 87.35.15.7/29 is not a broadcast address but it is. Or am i wrong somewhere? Or you do this trick only on the last 1 byte.?
  • Mike Scott
    Mike Scott almost 6 years
    @valer You've gone wrong. A /29 has three zero bits in the netmask, so the subnet size is 8 addresses, 2 ^ 3. The network address is thus 87.35.15.0, and the broadcast address is 87.35.15.7.
  • Mike Scott
    Mike Scott almost 6 years
    @valer Remember, an IPV4 address is a single 32-bit number that's conventionally written in dotted quads to make it easier to remember. That doesn't mean it's really four different 8-bit numbers.
  • valer
    valer almost 6 years
    Thanks i see now but won't this get harder if i have something like this 43.25.45.80/22 i see here that i need to find multiples of 2^10 and it gets messy? But it works if we have /24 or bigger (I mean quicker)
  • Mike Scott
    Mike Scott almost 6 years
    @valer If it's a /22, then since it goes in multiples of 1024 the third quad goes in multiples of four. So it's easy. The network address is 43.25.44.0.