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 192.168.0.9 with a mask of 255.255.255.0 I would have a subnet of 192.168.0.0/24 and I could just ping from 192.168.0.1 to 192.168.0.254 but then I realised, what about if I had an IP of 192.168.0.69 with a subnet netmask of 255.255.255.192. From this I'd have to ping hosts from 192.168.0.65 to 192.168.0.126 or 172.16.66.76 with netmask 255.255.192.0 which gives a host range of 172.16.64.1 to 172.16.127.254.
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.
Installation
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
Usage is simple, just pass the IP address and the subnet mask as parameters.
$ ./calc_ip_range.rb
get_range 1.0 Robin Wood (robin@digininja.org) (www.digininja.org)
Usage: get_range IP_ADDRESS NETMASK
--help, -h: show help
Example:
get_range 192.168.2.98 255.255.255.0