arping(8) solution:
#!/bin/sh if ( ( for i in {1..254}; do ( arping -q -c 1 -w 1 192.168.0.$i; echo $? ) & done; wait ) | grep -q -m 1 0 ) ; then echo "subnet has hosts" else echo "subnet is empty" fi
I'm sure it's possible to optimize that a bit further, but it seems adequate as-is. -Adam
On 2020-05-31 08:53, Adam Thompson wrote:
AFAIK, the solution is to not use nmap. I suspect arping would be just as useful for your case, and it runs nearly instantaneously. Couple with the Linux version's "-f" flag, put it in the background, collect all the return codes somehow[*] and you're done in under 0.5sec. I prefer arping because it eliminates stupid firewall problems. Or fping, which can scan an entire subnet in a second or so using ICMP in a single process in a couple of seconds. -Adam
On 2020-05-31 01:35, Trevor Cordes wrote:
Argh, sorry... for testing I was using a slightly bogus ip range... please change the 254-254 to 100-254. The corrected command line is below:
echo 192.168.101 | \ timeout 30 bash -c \ " \ sint=$(cat); \ (nmap -ddd -vvv --send-ip \ --host-timeout 5s --max-rtt-timeout 1s --initial-rtt-timeout 500ms \ --max-retries 1 -S $sint.1 -sn -PE $sint.2 $sint.100-254 2>&1 & \ echo $! >&3) \ 3>pidnmap \ | grep --line-buffered -P 'RCVD.{0,30}?ICMP.{0,60}?Echo reply' \ | (head -1 && kill $(<pidnmap) 2>&1) \ " \ |grep RCVD || echo "no hosts found"
And for some reason when I paste this from a my gui mail client it has syntax errors, but when I paste it from a tui mail client it works fine... YMMV. I guess I could post the one-line version:
echo 192.168.101 | timeout 30 bash -c "sint=$(cat); (nmap -ddd -vvv --send-ip --host-timeout 5s --max-rtt-timeout 1s --initial-rtt-timeout 500ms --max-retries 1 -S $sint.1 -sn -PE $sint.2 $sint.100-254 2>&1 & echo $! >&3) 3>pidnmap | grep --line-buffered -P 'RCVD.{0,30}?ICMP.{0,60}?Echo reply' | (head -1 && kill $(<pidnmap) 2>&1)" |grep RCVD || echo "no hosts found" _______________________________________________ Roundtable mailing list Roundtable@muug.ca https://muug.ca/mailman/listinfo/roundtable
Roundtable mailing list Roundtable@muug.ca https://muug.ca/mailman/listinfo/roundtable