编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
for
#!/bin/bash
NETID=10.0.0.
for HOSTID in {1..254};do
{ if /bin/ping -c1 -W1 $NETID$HOSTID >/dev/null ;then echo "$NETID$HOSTID is success" else echo "$NETID$HOSTID is fail" fi } & ###并发执行 done wait
while
#!/bin/bash NETID=10.0.0 declare -i HOSTID=1 while [ $HOSTID -lt 255 ];do ping -c1 -W1 $NETID.$HOSTID &> /dev/null if [ $? -eq 0 ];then echo "$NETID.$HOSTID success!" else echo "$NETID.$HOSTID fail!" fi let HOSTID++ done