Get the first free IP address in network

Hi all,
I’m trying to obtain the first free IP address in my network but something is going wrong. Assuming the the IP address I’m searching for is assigned to a machine, I obtain the right message (“IP not available”), but if the IP address is not assigned to a machine I obtain the error “Request timeout for icmp_seq 0” instead of the message “IP available”.
There is a way to change the timeout limit?


set theAddress to "192.168.1.110"
if (do shell script "ping -c 2 " & theAddress) is equal to "" then
	display dialog "IP available."
else
	display dialog "IP not available."
end if

Hello.

I think this should work. :slight_smile:

set ip_available to false
set theAddress to "192.168.1.110"
try
	do shell script "ping -c 2 " & theAddress
on error
	set ip_available to true
end try
if ip_available then
	display dialog "IP available."
else
	display dialog "IP not available."
end if

If you are playing with icmp packets and such, then you ought to know that there is WireShark for OS X, I don’t know if it is ready for Mavericks yet though. :confused:

Hi McUsrII,
thanks for your suggestion. I adapted the script to my needs and it works just fine.


set theAddress to "192.168.1."
set theFirst to 100
set theLast to 199

repeat with i from theFirst to theLast
	set theSearch to theAddress & i
	try
		set ping to (do shell script "ping -c 2 " & theSearch)
	on error
		exit repeat
	end try
end repeat

display dialog "The first available IP is : " & theSearch

I would use the options -o -t 1 with ping. It means it will quit when it receives an package and will quit after a timeout of 1 second. Then I redirect the output of ping to the null device (ignoreing it) and print only value “yes” or “no” according to if the ping was executed successfully. So you can get rid of the try-error which is something I don’t like.

set theAddress to "192.168.1."
set theFirst to 100
set theLast to 199

repeat with i from theFirst to theLast
	set theSearch to theAddress & i as string
	set hostOnline to (do shell script "ping -o -t 1 " & theSearch & " &>/dev/null && echo yes || echo no") as boolean
	if not hostOnline then exit repeat
end repeat

display dialog "The first available IP is : " & theSearch

Interestingly, this will fail if you are running Little Snitch, and it gives ridiculous answers if your LAN is of the 10.0.x.y type.

Hello.

First of all I think little Snitch must hijack your connection, in order to analyze your traffic, so it is kind of natural that it may clutter stuff.

I think it will give equally bad results Adam, if you have a 10.x.x gateway interface, and feed it 192.168.xxx. I think that is because those two interface addresses with your router, are pretty much standardized, so maybe there are some special program code in the network stack to deal with the two addresses. And I think the network stack (that is evrything from tcp, and down to the hardware layer, may take height for latency (temporarily dropped) connection, so it won’t be as determined with those addresses, as the others. It may even be a “cascade” back to the other kind of thing, to make both protocol works.

Well, that was the hypothesis, I am sure someone will correct me at least if I am very wrong. :wink: