28

I'm terrible at working out network subnets in my head. Is there some command line tool for linux (ubuntu packages a plus), that lets me put in 255.255.255.224 and it'll tell me that is a /27?

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253

6 Answers6

43

ipcalc can do this, for example:

[kbrandt@kbrandt-opadmin: ~] ipcalc 192.168.1.1/24                 
Address:   192.168.1.1          11000000.10101000.00000001. 00000001
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
Hosts/Net: 254                   Class C, Private Internet

if you entered a subnet mask instead of CIDR, you will still see the /## CIDR number after 'Network:', so it goes both ways.

or with sipcalc:

[kbrandt@kbrandt-opadmin: ~] sipcalc 192.168.1.1/24                                                                                             <23403@8:55>
-[ipv4 : 192.168.1.1/24] - 0
[CIDR]
Host address        - 192.168.1.1
Host address (decimal)  - 3232235777
Host address (hex)  - C0A80101
Network address     - 192.168.1.0
Network mask        - 255.255.255.0
Network mask (bits) - 24
Network mask (hex)  - FFFFFF00
Broadcast address   - 192.168.1.255
Cisco wildcard      - 0.0.0.255
Addresses in network    - 256
Network range       - 192.168.1.0 - 192.168.1.255
Usable range        - 192.168.1.1 - 192.168.1.254

The Ubuntu Packages are ipcalc and sipcalc:

sudo apt-get install ipcalc
sudo apt-get install sipcalc
Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
5

netmask supports automatically figuring out minimal sets of subnets for a particular IP range, which I find to be handy. For example:

# netmask -c 10.32.0.0:10.255.255.255
      10.32.0.0/11
      10.64.0.0/10
     10.128.0.0/9
wfaulk
  • 6,878
  • 7
  • 46
  • 75
5

You could use the bash scripts located here for converting from cidr to mask and mask to cidr notation:

Here's a copy of what the scripts are, to ensure the answer is always available here:

mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}


cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   echo ${1-0}.${2-0}.${3-0}.${4-0}
}

so for example, running:

mask2cdr 255.255.255.255 returns 32

Brad Parks
  • 713
  • 13
  • 20
3

I've used ipcalc before for this. It looks like Ubuntu also has sipcalc. See here.

Josh Kelley
  • 983
  • 1
  • 7
  • 17
2

Try either sipcalc or ipcalc.

Dan Carley
  • 25,617
  • 5
  • 53
  • 70
2

I use ipcalc for network subnetting, but it's limited to IPv4 addresses.
you can use subnetcalc, it support both of IPv4 and IPv6.

Ali Mezgani
  • 3,850
  • 2
  • 24
  • 36