首页 » shell编程 » 再上一道脚本面试题,比较实用

再上一道脚本面试题,比较实用

时间:2013-06-17作者:felix.chan分类:shell编程评论:5

试题 这是几年以前我发给一个老外请他写脚本程序的邮件原文 请按邮件内容将写好的脚本与简历发送至cup@lonlife.net 如果写的ok 我会联系你
I need a small program which can run in demon mode in linux just like ss5.it has only one feature: execute special commands when the ping delay changes or ping packets loss more than one special value.I want the program to change route when the delay is change or packets loss more than one special value.I can realize it via cacti and nagios,but cacti can only get the network data each 5 minutes,I need it to be real time, for example,we call this program lon01,and the config file is lon01.conf.then the config file is like this:

lon01.conf:

ip 203.78.180.88
delay 100 m CA
delay 80 b CB
loss 3 m CA
loss 20 b CB
CA ip ro re 203.66.0.0/16 via 1.21.18.1 dev sh01-tw02
CA iptables -t nat -I POSTROUTING -o eth0 -s 1.21.0.0/16 -j SNAT --to 61.160.250.42
CB ip ro re 203.66.0.0/16 via 1.20.18.1 dev sh01-tw03
CB iptables -t nat -I POSTROUTING -o eth0 -s 1.20.0.0/16 -j SNAT --to 61.160.250.42

the line ‘ip 203.78.180.88′ means the monitor ip,in this case,lon01 will ping 203.78.180.88 all the time.
the line ‘delay 100 m CA’ means when the ping delay is m(ore than) 100ms 3 continuous times,then execute CA(comandA) at the fourth seconds(fourth times of the ping) like this:

[root@sh01 ~]# ping us02.lonlife.net
PING us02.lonlife.net (66.152.183.130) 56(84) bytes of data.
64 bytes from 130-183-152-66-dedicated.multacom.com (66.152.183.130): icmp_seq=1 ttl=50 time=172 ms
64 bytes from 130-183-152-66-dedicated.multacom.com (66.152.183.130): icmp_seq=2 ttl=50 time=172 ms
64 bytes from 130-183-152-66-dedicated.multacom.com (66.152.183.130): icmp_seq=3 ttl=50 time=172 ms

“delay 80 b CB” means when the ping delay restore to normal,b(elow) 80ms 3 continuous times from changing to more than 100ms ,then execute CB(comandB)
“loss 3 m CA” means when the ping packets loss more than 3 packets,then execute CA(comandA) like this:

[root@fs04 ~]# p zz.lonlife.net
PING zz.lonlife.net (125.46.59.156) 56(84) bytes of data.
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=1 ttl=47 time=44.8 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=6 ttl=47 time=45.0 ms
 
--- zz.lonlife.net ping statistics ---
6 packets transmitted, 2 received, 66% packet loss, time 4998ms
rtt min/avg/max/mdev = 44.843/44.922/45.001/0.079 ms

“loss 20 b CB” means ping packets restore to normal(ping 203.78.180.88 0 loss more than 20 continuous times),then execute CB(comandB)
like this:

[root@fs04 ~]# p zz.lonlife.net
PING zz.lonlife.net (125.46.59.156) 56(84) bytes of data.
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=1 ttl=47 time=44.7 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=2 ttl=47 time=45.8 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=3 ttl=47 time=44.6 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=4 ttl=47 time=44.7 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=5 ttl=47 time=44.9 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=6 ttl=47 time=44.6 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=7 ttl=47 time=44.8 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=8 ttl=47 time=44.9 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=9 ttl=47 time=44.7 ms
64 bytes from hn.kd.ny.adsl (125.46.59.156): icmp_seq=10 ttl=47 time=45.6 ms

“CA ip ro re 203.66.0.0/16 via 1.21.18.1 dev sh01-tw02″ means command A,which will execute when delay is more than 100ms or loss more than 3 continuous times.
CB is like CA.

of course,when delay is more than 100ms,the order will execute for only one time.and when the delay is restore to normal(below 80ms),CB will been executed for only one time as well.flag loss is the same.delay and loss’ relationship is “or” not “and”.it means when delay is more than 100ms,execute CA,loss more than 3 continuous packets,run CA as well.it’s fine when the same command run two times.when delay is restore to normal,below 80ms from delay is more than 100ms),execute CB,or loss restore to normal(0 packtes loss 20 continuous times from 3 ping packets loss),execute CB as well.

well,after executing m(ore than,’delay 100 m CA’just like when ping 203.78.180.88 ‘s delay is more than 100ms,then executed CA),the b(elow,’delay 80 b CB’,when ping 203.78.180.88 ‘s delay restores to normal,below 80ms,execute CB’) can be triggered.I mean if the delay is always below 80ms,CB won’t execute.after delay changes to be more than 100ms and executed CA,when delay restore to normal,below 80ms,the CB can be executed for one time,if the delay is always below 80ms,CB won’s execute.

lon01.log

each time lon01 execute CA or CB,the lon01 should save log like this:
2010-10-17 1:19 ping 203.78.180.88's delay is 170ms(more than 100ms),execute CA "ip ro re 203.66.0.0/16 via 1.21.18.1 dev sh01-tw02" and "iptables -t nat -I POSTROUTING -o eth0 -s 1.21.0.0/16 -j SNAT --to 61.160.250.42"
2010-10-17 1:25 ping 203.78.180.88's delay restore to 85ms(below 80ms).execute CB "ip ro re 203.66.0.0/16 via 1.20.18.1 dev sh01-tw03" and "iptables -t nat -I POSTROUTING -o eth0 -s 1.20.0.0/16 -j SNAT --to 61.160.250.42"

thanks.I really need this program,please take some time to do this.I think this is a piece of cake for you.

答案如下:
抱歉,只有对本文发表过评论才能阅读隐藏内容。

原创文章,转载请注明: 转载自红防联盟www.rednat.com

本文链接地址: 再上一道脚本面试题,比较实用

|2|right
5 条评论
  1. avatar
    fengziyue China Internet Explorer Windows 2013-06-18 11:33 回复

    我想看看,呵呵

  2. avatar
    ck China Google Chrome Windows 2013-06-19 11:33 回复

    我想看看

  3. avatar
    liqius China Google Chrome Windows 2013-07-16 17:57 回复

    我想看看

  4. avatar
    76290615 China Mozilla Firefox Windows 2013-07-26 09:38 回复

    #!/bin/bash

    #lon01.conf formate
    #ip, 203.78.180.88
    #delay, 100, m ,echo “delay ca1 excute”
    #delay, 80, b, echo “delay cb1 excute”
    #loss, 3 ,m ,echo “loss ca2 excute”
    #loss, 20, b ,echo “loss cb2 excute”

    ipaddr=`head -1 lon01.conf | awk -F ‘,’ ‘{print $2}’`
    #echo $ipaddr ipaddr
    delaymore=`cat lon01.conf |sed -n ’2p’ |awk -F ‘,’ ‘{print $2}’`
    #echo $delaymore delaymore
    delaybelow=`cat lon01.conf |sed -n ’3p’ |awk -F ‘,’ ‘{print $2}’`
    #echo $delaybelow delaybelow
    lossmore=`cat lon01.conf |sed -n ’4p’ |awk -F ‘,’ ‘{print $2}’`
    #echo $lossmore lossmore
    lossbelow=`cat lon01.conf |sed -n ’5p’ |awk -F ‘,’ ‘{print $2}’`
    #echo $lossbelow lossbelow
    CA=`cat lon01.conf |sed -n ’2p’ |awk -F ‘,’ ‘{print $4}’`
    CB=`cat lon01.conf |sed -n ’3p’ |awk -F ‘,’ ‘{print $4}’`
    mdflag=1
    bdflag=1
    count=0
    while true
    do
    ping -c 1 $ipaddr >pingresult.txt
    sleep 0.1
    lossflag=`cat pingresult.txt |grep loss |awk -F ‘,’ ‘{print $2}’ |cut -b 2 `
    flag=0

    if [ $lossflag == 1 ];then

    {
    delaytime=`cat pingresult.txt |sed -n ’2p’ |awk -F ‘time=’ ‘{print $2}’ |awk ‘{print $1}’|awk -F ‘.’ ‘{print $1}’`
    # echo $delaytime delaytime

    if [ $delaytime -ge $delaymore ];then

    {
    if [ $mdflag == 1 ];then
    flag=1
    bdflag=1
    mdflag=0
    count=0
    else
    flag=0
    fi
    }
    elif [ $delaytime -le $delaybelow ];then
    {
    if [ $bdflag == 1 ];then
    flag=2
    mdflag=1
    bdflag=0
    count=0
    else
    flag=0
    fi
    }

    else
    flag=0
    fi }
    else
    let count=count+1
    # echo “Ping loss finds”
    {
    if [ $count == 4 ];then

    flag=3

    elif [ $count == 21 ];then

    flag=4

    else
    flag=0
    bdflag=1
    mdflag=1
    fi }

    ####
    fi

    case “$flag” in
    “1″ )
    $CA
    echo “`date +%Y-%m-%d” “%H:%M` ping $ipaddr’s delay is $delaytime(more than 100ms),execute \”CA ip ro re 203.66.0.0/16 via 1.21.18.1 dev sh01-tw02\” and \”iptables -t nat -I POSTROUTING -o eth0 -s 1.21.0.0/16 -j SNAT –to 61.160.250.42\” ” >>log.txt

    ;;
    “2″ )
    $CB
    echo “`date +%Y-%m-%d” “%H:%M` ping $ipaddr’s delay is $delaytime(more than 100ms),execute \”ping 203.78.180.88′s delay restore to 85ms(below 80ms).execute CB “ip ro re 203.66.0.0/16 via 1.20.18.1 dev sh01-tw03″ and \”iptables -t nat -I POSTROUTING -o eth0 -s 1.20.0.0/16 -j SNAT –to 61.160.250.42\” ” >>log.txt
    ;;
    “3″ )
    echo “ping loss 3 times ”
    echo “`date +%Y-%m-%d” “%H:%M` ping $ipaddr’s delay is $delaytime(more than 100ms),execute \”CA ip ro re 203.66.0.0/16 via 1.21.18.1 dev sh01-tw02\” and \”iptables -t nat -I POSTROUTING -o eth0 -s 1.21.0.0/16 -j SNAT –to 61.160.250.42\” ” >>log.txt
    ;;
    “4″ )
    echo “ping loss 20 times”
    echo “`date +%Y-%m-%d” “%H:%M` ping $ipaddr’s delay is $delaytime(more than 100ms),execute \”ping 203.78.180.88′s delay restore to 85ms(below 80ms).execute CB “ip ro re 203.66.0.0/16 via 1.20.18.1 dev sh01-tw03″ and \”iptables -t nat -I POSTROUTING -o eth0 -s 1.20.0.0/16 -j SNAT –to 61.160.250.42\” ” >>log.txt
    ;;
    * )
    # echo “Nothing change”
    # echo $count
    flag=0
    ;;
    esac

    done

  5. avatar
    test China Google Chrome Windows 2013-10-18 22:17 回复

    Copyright © 2011-2013 Rednat Linux网络日志平台 All rights reserved.
    Powered byWordPress. Theme byWordPress主题巴士.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>