View Single Post
  #2   (View Single Post)  
Old 16th August 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by bigb89 View Post
I would like to write a script (using perl or shell, etc) to find available IP addresses in my network.
The first thing to do is look at the manpage to ping(8) where you will find the necessary option to only send a specific number of ECHO_REQUEST datagrams before terminating. You should also look at the return code which will indicate ping's success of failure.

As for determining the current subnet, look at the output of ifconfig(8).

Once you have these two pieces of information, setting up a for-loop to iterate through all possible IP addresses should be reasonably straight-forward, but realize that you will also have to count according to the restrictions of dotted-decimal arithmetic when moving from one IP address to the next. Life would be simpler if ping would accept the 32-bit number which an IP address represents -- especially when you will need to increment upon every iteration of the for-loop to the next IP address, but I leave it to you to determine whether ping(8) accepts such a representation.

As for output, the simplest programmatically would be to output the results as they are generated, but this means that the output will be intermingled with ping's output unless you save the results of each test (or suppress ping's output by sending it to /dev/null...). My suggestion would be go with Perl given that setting up a hash keyed on IP address is more straight-forward.

Obviously, if you have a large subnet & send a sizable number of ECHO_REQUEST's, execution will take awhile. My suggestion is to work out the kinks on a single IP address first, & then scale up.
Reply With Quote