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.
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.
Using Nmap like this is a fairly accurate way of doing what you asked, provided that some preconditions are true:
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:
Summary of Nmap options used:
-v
option, Nmap will print the addresses it finds as "down" in addition to the ones that are "up".-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).-n
option skips reverse DNS lookups, which buys you a bit of time, since you aren't interested in names but just IP addresses.-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.
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.
Here is another one inspired by Anders Larsson
Which it means: "Try to ping all the Ips in the range. If the "ping" fails print that IP"
If you do
Works fast, but I noticed some hosts reported as "Down" are actually "Up"
Here's the same thing in PowerShell....