合格linux运维人员必会的30道shell编程实践题及讲解-05

企业面试题5:
写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)

我的脚本1==========================

[root@master day4]# cat ping_network.sh 
#!/bin/bash
. /etc/init.d/functions
uplist=/tmp/uplist.log
downlist=/tmp/downlist.log
[ -f $uplist ] || touch $uplist
[ -f $uplist ] && > $uplist
[ -f $downlist ] || touch $downlist
[ -f $downlist ] && > $downlist

for n in `seq 254`
do
    ping -c1 192.168.1.$n &>/dev/null
    if [ $? -eq 0 ];then
         action "192.168.1.$n is up" /bin/true |tee -a $uplist
    else
         action "192.168.1.$n is down" /bin/false |tee -a $downlist
    fi
done

我的脚本2======================

#!/bin/bash
IP=1
while [ $IP -le 255 ]; do
    ping -c 1 -w 2 192.168.1.$IP &>/dev/null
    STRING=$?
     if [ $STRING -eq 0 ];then
       action "192.168.1.$n is up" /bin/true |tee -a $uplist
    else
       action "192.168.1.$n is down" /bin/false |tee -a $downlist
    fi
    let IP=$IP+1
done
我的脚本3====================
#!/bin/bash
. /etc/init.d/functions
uplist=/tmp/uplist.log
downlist=/tmp/downlist.log
[ -f $uplist ] || touch $uplist
[ -f $uplist ] && > $uplist
[ -f $downlist ] || touch $downlist
[ -f $downlist ] && > $downlist
for((ip=1;ip<255;ip++))
do
    ping -c1 192.168.1.$ip &>/dev/null
    if [ $? -eq 0 ];then
         action "192.168.1.$ip is up" /bin/true |tee -a $uplist
    else
         action "192.168.1.$ip is down" /bin/false |tee -a $downlist
    fi
done
 
原文地址:https://www.cnblogs.com/oliver-blogs/p/7715865.html