全部博文(2005)
分类:
2008-12-28 20:08:11
# 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" |