阿铭每日一题 day 11 20180121

 

 

 

方法一:

1 #!/bin/bash
2 
3 ip="192.168.0"
4 for i in `seq 1 254`
5 do
6   ping -c 2 $ip.$i |grep -q "ttl=" && echo "$ip.$i up..." >> /tmp/ping_ip.txt || echo "$ip.$i down..." >> /tmp/ping_ip.txt
7 done

 

 

方法二:

#! /bin/bash  
for siteip in $(seq 1 254)  
do  
    site="192.168.0.${siteip}"  

#-c的意思ping的次数
#-n在输出数据时不进行IP与主机名的反查,直接使用IP输出速度快
#-W 等待响应对方主机的秒数
    ping -c1 -W1  ${site} &> /dev/null  
    if [ "$?" == "0" ]; then  
        echo "$site is up" > /tmp/pingfile  
    else  
        echo "$site is down"  
    fi    
done 

 

 

day11参考答案:

#!/bin/bash

ips="192.168.1."
for i in `seq 1 254`
do

ping -c 2 $ips$i >/dev/null 2>/dev/null
if [ $? == 0 ]
then
    echo "echo $ips$i is online"
else
    echo "echo $ips$i is not online"
fi
done

 

posted @ 2018-01-22 15:41  Ivan_yyq  阅读(225)  评论(0编辑  收藏  举报