ping网段的脚本
ping 网段的脚本
#!/bin/bash
the_up=0
the_down=0
for i in {1..254}
do
ping -c 3 -i 0.2 -w 1 192.168.10.$i >> /dev/null
if [ $? -eq 0 ] ; then
echo "192.168.10.$i 通了"
let the_up++
else
echo "192.168.10.$i 没通"
let the_down++
fi
done
echo "有 $the_up 个通"
echo "有 $let_down 个没有通"
解释
先定义了两个变量 the_up 和 the_down 用于统计网络ping通和没有ping通的此次数。
{1..254} 用于生成数字:
[root@server ~]# echo {5..10}
5 6 7 8 9 10
$? 用于返回上次命令的执行结构,如果bash中的上个命令执行成功那么 $? 的只就是 0,如果失败就是其他的数字。
[root@server ~]# echo "good" > test.txt
[root@server ~]# cat test.txt
good
[root@server ~]# echo $?
0
==========================================
[root@server ~]# cat test1.txt
cat: test1.txt: No such file or directory
[root@server ~]# echo $?
1
上面的 $? 是 0 表示的就是上次命令成功了,而下面的 $? 的是 1 就表示上次命令失败了。
[ $? -eq 0 ] 用于判断上个命令是否成功了。

浙公网安备 33010602011771号