Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15529430
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类:

2008-12-28 20:08:11

《可以在busybox-1.12.0下跑的精简了很多东西的shell脚本》
《定制shell脚本在busybox中运行》
因为busybox-1.12.0好多功能都没能实现,所以只能采取折中方式,使用一些手段达到我们的目的,下面这段代码可以实现顺序执行输入的命令,同时也可以实现random随机抽取一个命令随机执行,这样可以实现我们的无人时的网络测试目的:)[luther.gliethttp]
# Copyright 2008 luther.gliethttp
#============================================================================
lu_list_commands=
lu_subcmd=

lu_search_sub() {
    local iindex=$1
    local lu_strings="$2"
    local do_quit=0
    local pos=
    local num=0

    while [ $do_quit -ne 1 ]; do
        num=`expr $num + 1`
        pos=`expr index "$lu_strings" ';'`
        if [ $? -eq 0 ]; then
            nextpos=`expr $pos + 1`
            if [ $num -eq $iindex ]; then
                prevpos=`expr $pos - 1`
                lu_subcmd=`echo $lu_strings | cut -c1-$prevpos`
                do_quit=1
                continue
            fi
            lu_strings=`echo $lu_strings | cut -c$nextpos-`
        else
            if [ $num -eq $iindex ]; then
                lu_subcmd="$lu_strings"
            else
                lu_subcmd=''
                return 1
            fi
            do_quit=1
        fi        
    done
    return 0
}

lu_print_cmd() {
    echo '==========================================='
    echo "[$1:$2] $3"
}

lu_do_action() {
    local cmd_nums=$1
    local cmds="$2"
    local index=
    local do_quit=0

    until [ $do_quit -ne 0 ]; do
        index=0
        while [ $index -le $cmd_nums ]; do
            index=`expr $index + 1`
            # lu_search_sub $index "$cmds" && echo "$lu_subcmd"
            lu_search_sub $index "$cmds" || continue
            lu_print_cmd $index $cmd_nums "$lu_subcmd"
            $lu_subcmd
        done
        [ -z $laping_loops ] && do_quit=1
    done
}

lu_do_action_one() {
    local index=$1
    local cmd_nums=$2
    local cmds="$3"
    lu_search_sub $index "$cmds" || continue
    lu_print_cmd $index $cmd_nums "$lu_subcmd"
    $lu_subcmd    
}

lu_drain_commands() {
    local cmd_nums=$1
    local cmds="$2"
    local index=0
    local random_index=

    if [ -z $laping_random ]; then
        lu_do_action $cmd_nums "$cmds"
    else
        while [ 'x' ]; do
            random=$RANDOM
            [ -z $random ] && echo 'no $RANDOM' && sleep 1 && continue
            if [ -n $random ]; then
                # echo $random
                random_index=`expr $random % $cmd_nums + 1`
                lu_do_action_one $random_index $cmd_nums "$cmds"
            else
                lu_do_action $cmd_nums "$cmds"
            fi
        done
    fi
    # lu_search_sub 1 "$cmds" && echo $lu_subcmd
    # lu_search_sub 2 "$cmds" && echo $lu_subcmd
    # lu_search_sub 3 "$cmds" && echo $lu_subcmd
    # lu_search_sub 0 "$cmds" && echo $lu_subcmd
    # lu_search_sub 4 "$cmds" && echo $lu_subcmd
    return 0   
}

lu_laping() {
    # echo $@
    shift
    local maxcounts=$1
    local sleep_seconds=$2
    local pingcounts=$3
    local interval=$4
    local ip_addr=$5

    while [ $maxcounts -gt 0 ]; do
        ping -c $pingcounts $ip_addr -i $interval >/dev/null 2>&1
        if [ $? -gt 0 ]; then
            echo 'wrong'
        else
            echo 'pass'
        fi
        sleep $sleep_seconds
        maxcounts=`expr $maxcounts - 1`
    done
}

Usage() {
    # cat <<\_EOF
    cat <<_EOF

Usage:
`basename $0`
              -h    help
              -t    run seconds
              -l    looping does not exit
              -r    do commande randomly
              -a[maxcounts],[sleep_seconds],[pingcounts],[interval],[ip_addr]
sample:

./random_ping.sh -l -a 5,1,1,1,127.0.0.1 -a 1,1,1,1, -a 2,1,1,1,gliethttp.cublog.cn -a 3,1,1,1,127.0.0.1 -a 4,1,1,1,
_EOF
    echo
    return 0
}

while getopts lrha: op; do
    case $op in
        'h')
            Usage
            exit 0
        ;;
            
        't')
            LU_SECONDS=$OPTARG
            echo $LU_SECONDS | grep -q '[^0-9]' && Usage && exit 1
            [ $LU_SECONDS -eq 0 ] && LU_SECONDS=$((0xffffffff))
        ;;

        'r')
            laping_random=1
        ;;

        'l')
            laping_loops=1
        ;;
            
        'a')
            [ -z $lapingindex ] && lapingindex=0
            substring=$OPTARG
            # echo `expr index "$substring" ','`
            substring=`echo $substring | tr ',' ' '`
            # ${substring//','/' '}
            if [ $lapingindex -ne 0 ]; then
                lu_list_commands=$lu_list_commands';'
            fi
            lu_list_commands=$lu_list_commands"lu_laping"' '"$lapingindex"' '"$substring"
            lapingindex=`expr $lapingindex + 1`
            # echo '==========================================='
            # echo "$substring"
            # lu_laping $lapingindex $substring
        ;;

        *)
        ;;
    esac    
done    

# echo $lu_list_commands
# ($lu_list_commands)
# (lu_laping 0 2 1 1 1 127.0.0.1;lu_laping 0 2 1 1 1 127.0.0.1)
[ -z $lapingindex ] && Usage && exit 1
lu_drain_commands $lapingindex "$lu_list_commands"
luther@gliethttp:/vobs$ ./random_ping.sh -r -a 5,1,1,1,127.0.0.1 -a 1,1,1,1,127.0.0.1 -a 2,1,1,1,127.0.0.1 -a 3,1,1,1,127.0.0.1 -a 4,1,1,1,127.0.0.1
===========================================
[1:5] lu_laping 0 5 1 1 1 127.0.0.1
pass
pass
pass
pass
pass
===========================================
[1:5] lu_laping 0 5 1 1 1 127.0.0.1
pass
pass
pass
pass
pass
===========================================
[1:5] lu_laping 0 5 1 1 1 127.0.0.1
pass
pass
pass
pass
pass
===========================================
[2:5] lu_laping 1 1 1 1 1 127.0.0.1
pass
===========================================
[1:5] lu_laping 0 5 1 1 1 127.0.0.1
pass
pass
pass
pass
pass
===========================================
[1:5] lu_laping 0 5 1 1 1 127.0.0.1
pass
pass
pass
pass
pass
===========================================
[3:5] lu_laping 2 2 1 1 1 127.0.0.1
pass
pass
===========================================
[5:5] lu_laping 4 4 1 1 1 127.0.0.1
pass
pass
pass
pass
===========================================
[4:5] lu_laping 3 3 1 1 1 127.0.0.1
pass
pass
pass
===========================================
[3:5] lu_laping 2 2 1 1 1 127.0.0.1
pass
pass
===========================================
[4:5] lu_laping 3 3 1 1 1 127.0.0.1
pass
pass
pass
===========================================
[2:5] lu_laping 1 1 1 1 1 127.0.0.1
pass
===========================================
[2:5] lu_laping 1 1 1 1 1 127.0.0.1
pass
===========================================
[4:5] lu_laping 3 3 1 1 1 127.0.0.1
pass
pass
pass
===========================================
[3:5] lu_laping 2 2 1 1 1 127.0.0.1
pass
...

阅读(3102) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~