• 合格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
     
  • 相关阅读:
    .ini文件的介绍及对其进行操作
    一些.net 控件使用的小细节
    三、类型设计规范
    [转]TimerCallback 委托
    [转]简单XML文件C#操作方法
    [转]用托盘控制windows服务的c#实现
    [转]DateTime相关
    [转]创建Windows服务 C#
    一、框架设计的基础
    [转]得到当前执行的函数名、码行、源代码文件名
  • 原文地址:https://www.cnblogs.com/oliver-blogs/p/7715865.html
Copyright © 2020-2023  润新知