Friday, October 10, 2008

Find Subnet Mask with ipcalc

An easy way to convert a subnet mask from bit notation (e.g. /24) to dotted decimal notation is to use ipcalc from any linux box. The -m option displays the subnet mask.

$ ipcalc -m 10.10.10.128/25
NETMASK=255.255.255.128
$

O.K., that's fine, you say, but how do I know how many hosts are on the subnet? That's easy enough -- just use the -b option to find the broadcast address.

$ ipcalc -b 10.10.10.128/25
BROADCAST=10.10.10.255

The range of IP's will be from one more than the network address up to one less than the broadcast address. In the example above the IP address range is 129-254. Often the gateway is the first or last address in the range to avoid confusion.

If you don't have access to a linux box, you can always do a google search for "subnet calc," where you can probably find an online subnet calculator.

For the opposite calculation - find prefix from subnet mask, use the -p option.

$ ipcalc -p 10.10.10.128 255.255.255.128
PREFIX=25
$

There you go. You don't need any special software, as ipcalc is probably already installed in /bin. On RHEL5 it is part of initscripts.

On RHEL4
$ which ipcalc
/bin/ipcalc
$ rpm -qf /bin/ipcalc
initscripts-7.93.31.EL-2
$

On RHEL5
$ rpm -qf /bin/ipcalc
initscripts-8.45.19.EL-1
$