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
     
  • 相关阅读:
    [leetcode] Reverse Linked List II
    利用ServletContextListener实现定时任务
    以追加方式写入文件的几种方法
    序列化反序列化的几种方式
    最常用快捷键
    Eclipse快捷键大全
    【MongoDB for Java】Java操作MongoDB
    JQuery EasyUI window 用法
    Oracle sql 性能优化调整
    Jodd 3.3
  • 原文地址:https://www.cnblogs.com/oliver-blogs/p/7715865.html
Copyright © 2011-2022 走看看