|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
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! |
|
|||
Quote:
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. |
|
|||
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..) |
|
|||
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. |
|
|||
I don't believe so.. never used any of the utilities related to poisoning, only have a few systems on my subnet anyway.
|
|
|||
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 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. |
|
|||
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. |
|
|||
Quote:
Code:
ping -c 1 $ip.$val > /dev/null if [ "$?" -eq 0 ] ; then Just like I needed! Now, I'll probably be expanding this script to perhaps add more functionality to it. |
|
||||
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 |
|
|||
agr... this was the first form of the script - the second one was with case and it work fine ..
Quote:
__________________
Verbose mode can also be turned on for SSH2 with the (surprise!) VerboseMode keyword. |
|
|||
Quote:
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? |
|
||||
Quote:
Code:
nmap -sP -v -v 192.168.2.0/24 |
|
|||
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. |
Thread Tools | |
Display Modes | |
|
|
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 |