DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 16th August 2008
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Default How to find available IP addresses?

Hi guys,

I would like to write a script (using perl or shell, etc) to find available IP addresses in my network. I really don't know where to start.

Here's what I would like the script to do:

Ping (or any other tool that may work) a range of IPs (example: 0.0.0.1 to 0.0.0.255). Then whenever an IP replies, it means that its already being used somewhere so this IP will be sent to a file called usedIP, but if the IP gives no response, then chances are that the IP is not being used. So this IP will be sent to a file called availableIP.

Could you guys give me some hint/help on how I can accomplish this?

Cheers!
Reply With Quote
  #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
  #3   (View Single Post)  
Old 16th August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

Actually, I would go in a different direction.. ping can be useful, but so often is ICMP Echo blocked by firewalls.

Why not use ARP instead? there are a few portable utilities out there.. in C.

ARPing
ARP Tools

The first is in OpenBSD ports, the second.. isn't... it also includes some, spoofing utilities.. but the arpdiscover program should be adequate.

Both use libpcap and libnet, the latter being in ports.. the former in base (usually..)
Reply With Quote
  #4   (View Single Post)  
Old 16th August 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Default

Quote:
Originally Posted by BSDfan666 View Post
Actually, I would go in a different direction.. ping can be useful, but so often is ICMP Echo blocked by firewalls.

Why not use ARP instead? there are a few portable utilities out there.. in C.
AFAIK,, arp table needs some time to update, so if a host comes online, its entry may not appear in the arp table instantly. Also, for the hosts isnt in the same subnet, the arp method doesnt work. ARP operates at layer 2, you know

For host detection purpose, I would choose nmap
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #5   (View Single Post)  
Old 16th August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

I'm going to retract my previous post for now, both utilities seem to.. lock up connectivity after usage, quite odd.

Could be an OpenBSD issue, but.. I guess ping might be a more viable.. yet noisy, solution.
Reply With Quote
  #6   (View Single Post)  
Old 16th August 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Default

Quote:
Originally Posted by BSDfan666 View Post
I'm going to retract my previous post for now, both utilities seem to.. lock up connectivity after usage, quite odd.
arp poisoning?
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #7   (View Single Post)  
Old 16th August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

I don't believe so.. never used any of the utilities related to poisoning, only have a few systems on my subnet anyway.
Reply With Quote
  #8   (View Single Post)  
Old 16th August 2008
arch arch is offline
Port Guard
 
Join Date: Jun 2008
Posts: 38
Default

about the arp stuff try arpwatch - it's harvest mac addresses in combination with ip - one drawback is that it omit zeros if you have mac for exmp: 0b:bla:bla ... it print it in the log in form of b:bla:bla.
btw for a week now i try some sh scripting and .. want to share :-p
here it is:

Code:
#!/bin/sh

val=1
count=255

echo input ip range
read ip

while [ $val -le $count ]
do
   echo ping ip $ip.$val
   `/sbin/ping -c 1 $ip.$val > /dev/null 2>&1`
   if echo $?
then
        echo ip is in use
        echo $ip.$val >> used
else
        echo ip is not in use
        echo $ip.$val >> unused
fi
        val=`expr $val + 1`
done
i know..it's lame.. :-p
P.S by ip range i mean..ip block e.g you input 192.168.0 and that's it.. to stop it you have to suspend it.. or killed or whatever..
if use ctr+c well.. you fast though all ip's
__________________
Verbose mode can also be turned on for SSH2 with the (surprise!) VerboseMode keyword.

Last edited by arch; 16th August 2008 at 06:07 PM.
Reply With Quote
  #9   (View Single Post)  
Old 16th August 2008
Weaseal's Avatar
Weaseal Weaseal is offline
Package Pilot
 
Join Date: May 2008
Location: East Coast, US
Posts: 177
Default

Maybe look into nmap? It can do many types of scans of IP ranges.
__________________
FreeBSD addict since 4.2-RELEASE.
My FreeBSD wiki.
Reply With Quote
Old 16th August 2008
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Default

Thanks for all the replies guys!

Yes, I have heard that nmap could be the way to go on this. I'll definitely check it out. In the mean time, I have read a little on ping man page, and I have done a couple of google searches where I came across the following things than may help me on starting this script:

1) You can use the -c option to determine the amount requests you want ping to send.
2) Ping returns 1 if no replies are received.

In the end, arch's script is doing almost exactly what I need using the above useful information that I found. That, script may be simple, but in the end it does what it needs to do .

I'll be looking into nmap or any other solutions, and I'll even be trying out your script arch (if that's OK with of course).

The only thing that still concerns me, is if ping is blocked in the firewall of certain host. That's where I guess nmap will come in handy.
Reply With Quote
Old 17th August 2008
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Default

Quote:
Originally Posted by arch View Post
about the arp stuff try arpwatch - it's harvest mac addresses in combination with ip - one drawback is that it omit zeros if you have mac for exmp: 0b:bla:bla ... it print it in the log in form of b:bla:bla.
btw for a week now i try some sh scripting and .. want to share :-p
here it is:

Code:
#!/bin/sh

val=1
count=255

echo input ip range
read ip

while [ $val -le $count ]
do
   echo ping ip $ip.$val
   `/sbin/ping -c 1 $ip.$val > /dev/null 2>&1`
   if echo $?
then
        echo ip is in use
        echo $ip.$val >> used
else
        echo ip is not in use
        echo $ip.$val >> unused
fi
        val=`expr $val + 1`
done
i know..it's lame.. :-p
P.S by ip range i mean..ip block e.g you input 192.168.0 and that's it.. to stop it you have to suspend it.. or killed or whatever..
if use ctr+c well.. you fast though all ip's
Arch, I ran your script and I found a problem with it. Every time that I ran your script, it showed that all IPs were being used even though some IPs were not being used. Turns out that the problem was with your "if statement". You are not checking to see whether ping returned 0 (ping got a reply) or 1 (ping did not get a reply). So to fix that I went ahead and corrected your "if statement" and did the following:

Code:
ping -c 1  $ip.$val > /dev/null

if [ "$?" -eq 0 ] ; then
After changing the "if statement", your script is working great!!!
Just like I needed!
Now, I'll probably be expanding this script to perhaps add more functionality to it.
Reply With Quote
Old 17th August 2008
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 192
Default

I would certainly go with nmap, it's got several types of scans: ping scan, TCP {syn,ack} scan, UDP scan, arp scan...and it supports CIDR-style scanning like 192.168.1.0/24, or for example 192.168.1.1-150. See: http://nmap.org/book/man-host-discovery.html
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD
Reply With Quote
Old 17th August 2008
Weaseal's Avatar
Weaseal Weaseal is offline
Package Pilot
 
Join Date: May 2008
Location: East Coast, US
Posts: 177
Default

Whenever we have clients that have connectivity issues the first thing I do is "nmap -sP 192.168.2.0/24"

/24 = 192.168.2.1 - 192.168.2.254

-sP just means do nothing other than a ping scan to determine if the host exists.
__________________
FreeBSD addict since 4.2-RELEASE.
My FreeBSD wiki.
Reply With Quote
Old 17th August 2008
arch arch is offline
Port Guard
 
Join Date: Jun 2008
Posts: 38
Default

agr... this was the first form of the script - the second one was with case and it work fine ..

Quote:
Originally Posted by bigb89 View Post
Arch, I ran your script and I found a problem with it. Every time that I ran your script, it showed that all IPs were being used even though some IPs were not being used. Turns out that the problem was with your "if statement". You are not checking to see whether ping returned 0 (ping got a reply) or 1 (ping did not get a reply). So to fix that I went ahead and corrected your "if statement" and did the following:
__________________
Verbose mode can also be turned on for SSH2 with the (surprise!) VerboseMode keyword.
Reply With Quote
Old 19th August 2008
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Default

Quote:
Originally Posted by Weaseal View Post
Whenever we have clients that have connectivity issues the first thing I do is "nmap -sP 192.168.2.0/24"

/24 = 192.168.2.1 - 192.168.2.254

-sP just means do nothing other than a ping scan to determine if the host exists.
Thanks for tip!

I tried using this command and it gave me a list of hosts that were up. Now do you know of anyway to lists the hosts that are down instead?
Reply With Quote
Old 19th August 2008
Weaseal's Avatar
Weaseal Weaseal is offline
Package Pilot
 
Join Date: May 2008
Location: East Coast, US
Posts: 177
Default

Quote:
Originally Posted by bigb89 View Post
I tried using this command and it gave me a list of hosts that were up. Now do you know of anyway to lists the hosts that are down instead?
Add the -v flag for verbose. You can also add it a second time for extra verbosity:
Code:
nmap -sP -v -v 192.168.2.0/24
The man page is your friend
__________________
FreeBSD addict since 4.2-RELEASE.
My FreeBSD wiki.
Reply With Quote
Old 20th August 2008
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Default

Thanks for the help Weaseal!

That did exactly what I needed, then all I had to do was just "grep" the hosts that were down.

I did check man pages for nmap and I saw the -v for verbose. I just thought that there were another command that would report only host down, and I could not find that.

Anyways, that did help me alot, and I was able to get a list all IPs available that I had.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
K3b cannot find growisofs maxrussell FreeBSD General 5 26th April 2009 12:20 PM
pkg_add g95;g95 x.f95: cannot find g95 enpey OpenBSD Packages and Ports 8 27th August 2008 12:48 AM
where might I find 'libcamel'? Damien787 FreeBSD Ports and Packages 16 17th June 2008 11:35 PM
Managing IP Addresses bigb89 FreeBSD General 8 28th May 2008 12:09 AM
abbreviating email addresses? ocicat Feedback and Suggestions 9 22nd May 2008 12:21 AM


All times are GMT. The time now is 06:33 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick