Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a way to scan for free IPs on the network? I use nmap -sP 192.168.1.0/24 but this actually shows hosts that are up.

share|improve this question
8  
doesn't respond to ping doesn't necessarily mean unused. –  Grant Apr 4 at 13:34
1  
You want an IPAM scanner, not nmap. –  TheCleaner Apr 4 at 13:39
2  
@Grant Nmap's host discovery scan (sometimes called "ping scan") sends lots of different probes, not just ICMP Echo Requests. For addresses on the same data link, it uses ARP requests, which are basically impossible to ignore. –  bonsaiviking Apr 4 at 13:59
1  
You should also remember that hosts that are not powered on will also show up as not having an IP address. –  Tero Kilkanen Apr 4 at 14:07

2 Answers 2

up vote 9 down vote accepted

Using Nmap like this is a fairly accurate way of doing what you asked, provided that some preconditions are true:

  1. You must run the scan as root (or Administrator on Windows) in order to send ARP requests, not TCP connections. Otherwise the scan may report an address as "down" when it is simply firewalled.
  2. You can only do this from a system on the same data link (layer 2) as the address range you are scanning. Otherwise, Nmap will need to use network-layer probes which can be blocked by a firewall.

In order to get the "available" addresses, you need to get the list of addresses that Nmap reports as "down." You can do this with a simple awk command:

sudo nmap -v -sn -n 192.168.1.0/24 -oG - | awk '/Status: Down/{print $2}'

Summary of Nmap options used:

  • When you use the -v option, Nmap will print the addresses it finds as "down" in addition to the ones that are "up".
  • Instead of -sP, I've substituted the newer spelling -sn, which still accomplishes the same scan, but means "skip the port scan" instead of the misleading "Ping scan" (since the host discovery phase does not necessarily mean an ICMP Echo scan or Ping).
  • The -n option skips reverse DNS lookups, which buys you a bit of time, since you aren't interested in names but just IP addresses.
  • The -oG option tells Nmap to output grepable format, which is easier for awk to process. The argument "-" tells it to send this output to stdout.

The awk command then searches for "Status: Down" and prints the second field, containing the IP address.

Of course, if you have access to the switch's running configs or the DHCP server's leases, you could get this answer much more authoritatively without doing a scan that could set off security alarms.

share|improve this answer
1  
Totally bookmarking this for future use and its not even my question :) –  MartinC Apr 4 at 13:59

Not sure about n-map, but one could reasonably assume that if you write a ping script that sends 1 ping to each address that any hosts that come back with "destination unreachable" is unoccupied, and anything that comes back "request time out" is occupied but not responding to ping. The difference between the two responses is that "destination unreachable" did not receive a response to its ARP request. "Request time out" means something did respond to the ARP request, but not the ICMP packet.

share|improve this answer
    
When run with root privilege, Nmap simply does the ARP request and reports addresses that respond. –  bonsaiviking Apr 4 at 13:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.