zoukankan      html  css  js  c++  java
  • 合格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
     
  • 相关阅读:
    CSS font-family 字体名称一览表
    jQuery动态追加移除CSS样式
    Java中super关键字的作用与用法
    I Have a Dream(我有一个梦想)
    再读《诫子书》
    世间谤我、欺我、辱我、笑我,为之奈何?
    英语句型:我除了音乐一无所能
    H5 iphoneX适配方案
    对象序列化成字符串,拼接在请求url后面
    React 60s倒计时
  • 原文地址:https://www.cnblogs.com/oliver-blogs/p/7715865.html
Copyright © 2011-2022 走看看