#!/bin/bash
#这是父进程#
>used_ip.txt
>unused_ip.txt
for i in {1..255}
do
#()&生成多个子进程并行,提高ping的速度效率
(ping 192.168.0.$i -c 1 -w 1 &>/dev/null
#ping 1次,时长为1s必须完成,然后将结果丢进黑洞文件
if(($? == 0))#如果ping通,ping通的返回值为0.
then
echo "192.168.0.$i is used"
echo "192.168.0.$i" >>used_ip.txt
else
echo "192.168.0.$i" >>unusedip.txt
fi)&
done
wait #明显父进程清空文件简单快速,所以父进程运行完需等待子进程
echo '########[used ip detail]################'
cat used_ip.txt
echo '#######[arp buffer table]###############'
num=$(cat used_ip.txt|wc -l)
echo "一共有$num个ip地址"