一.练习
1.ping命令去查看192.168.3.1-192.168.3.50范围内的所有主机是否在线;在线的显示为up, 不在线的显示down,分别统计在线主机,及不在线主机数;分别使用for, while和until循环实现。
for循环
#!/bin/bash
declare -i up=0
declare -i down=0
for i in {
1..50};do
if ping -W 1 -c 1 192.168.3.$i &> /dev/null;then
echo "the hostname 192.168.3.$i up"
let up++
else
echo "the hostname 192.168.3.$i down"
let down++
fi
done
echo "Up hosts:$up;Down hosts:$down"
使用函数及while实现
#!/bin/bash
declare -i up=0
declare -i down=0
declare -i i=1
hostping() {
if ping -W 1 -c 1 192.168.3.$i &> /dev/null;then
echo "the hostname 192.168.3.$i up"
return=0
else
echo "the hostname 192.168.3.$i down"
return=5
fi
}