Calc IP Range

This short script was created because I needed a way to be able to ping every IP address in a given subnet without using a broadcast ping. Why no broadcast? Because apparently some switches will block broadcast pings so my scan might not get to every machine on the subnet if there are some switches in the way.

To make it a little more tricky, all I had to go on is the IP address of one machine in the subnet and the subnet mask.

My initial thought was that it would be simple, for the IP with a mask of I would have a subnet of and I could just ping from to but then I realised, what about if I had an IP of with a subnet netmask of From this I'd have to ping hosts from to or with netmask which gives a host range of to

A quick Twitter shout got me a lot of pointers to online subnet calculators which manually gave me the answers I'd need but didn't offer a way to script finding the top and the bottom of the range, then Carlos Perez came to the rescue and pointed me at the ruby gem "ip". This great little gem will take an IP and subnet mask and from it give all the information I needed, top and bottom host address, network and broadcast address.

The script I've released shows the information and then does the ping and exits, I'll leave it to your imagination what to do from then onwards or what other commands you can replace the ping with.


You should only need to install the ip gem which can be done as root by running

gem install ip

Some people may also need to set the following

export RUBYOPT="rubygems"


Usage is simple, just pass the IP address and the subnet mask as parameters.

$ ./calc_ip_range.rb 
get_range 1.0 Robin Wood ( (

Usage: get_range IP_ADDRESS NETMASK
	--help, -h: show help



download calc ip range

Support The Site

I don't get paid for any of the projects on this site so if you'd like to support my work you can do so by using the affiliate links below where I either get account credits or cash back. Usually only pennies, but they all add up.