host discovery using bash

I am trying to make a bash script to scan subnets to see what hosts are available. Is it correct that you can not make the ping command time out less than a second? The script below works, but can take up to 255 seconds which is a bit long :frowning: Is there a way to solve this using bash only?

<code>
subnet=192.168.2.
addr=1
while [ $addr -lt 256 ]; do
ping -c 1 -t 1 $subnet$addr > /dev/null && echo Found $subnet$addr
let addr=addr+1
done
</code>

I think I saw a program called "uping" or "mping" which had more fine-grained timing controls (I think part of the MRTG suite ... could have been "fastping" too).

Two other ideas come to mind.

  1. Run asynchronously. Spawn off a big bunch of pings and even if some of them take one second or more, the whole bunch will finish in one or two seconds (provided you have the CPU and bandwidth to run enough of them in parallel).
subnet=192.168.2.
for addr in `seq 1 1 255 `; do
  ( ping -c 1 -t 1 $subnet$addr > /dev/null && echo Found $subnet$addr ) &
done
  1. Is ping really what you want? If you have a hunch about a port you could expect to be open, netcat could do this much faster (and probably produce more accurate results -- the fact that ping works doesn't really mean the host is up and working correctly).

I think this is the "fast ping" I was thinking of. fping.com Looks like it can handle the whole problem you have.

It's used by a system called SmokePing, by the MRTG author, wich is however apparently a separate module. SmokePing - Smokeping::probes::FPing

Era, I am so sorry for the late reply.

Your script is perfect! Thanks very much for the input.
I really just want to know where my machines are on the network . My DHCP server is so basic that it does not even show me the current leases.

If it's your network then you could also simply do a ping to the broadcast address, although I guess a some modern machines will no longer respond to broadcast ping (at least it's an option).

If i were you, i would have nmap

~s3g4

@era... ah yes simple command even better. I did not know you could do a broadcast ping :noob: All my machines respond to ping -b 192.168.2.255 so this works just fine for me.

Thanks again