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
     
  • 相关阅读:
    一种C#读写二进制文件的通用方法
    关于POP3协议的一点资料
    关于看图工具的几点想法
    在WPF程序中将控件所呈现的内容保存成图像
    Nuget挂了的解决方法
    VisualStudio 2012中的单元测试
    在Andorid平板上体验Windows8的猜想
    创建自己的awaitable类型
    【转载】:最佳注释
    百度云盘试用
  • 原文地址:https://www.cnblogs.com/oliver-blogs/p/7715865.html
Copyright © 2011-2022 走看看