加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – 错误的数组下标

发布时间:2020-12-16 01:34:29 所属栏目:安全 来源:网络整理
导读:我对 Shell比较陌生,我一直在尝试在Shell中使用数组,但效果不佳.我正在尝试编写一个脚本来暂停OpenVZ容器,如果它们在一段时间内达到一定的带宽阈值. 我的剧本: #!/bin/bash#Thresholds are in bytes per second and count1 must count2LOGDIR="/var/log/out
我对 Shell比较陌生,我一直在尝试在Shell中使用数组,但效果不佳.我正在尝试编写一个脚本来暂停OpenVZ容器,如果它们在一段时间内达到一定的带宽阈值.

我的剧本:

#!/bin/bash
#Thresholds are in bytes per second and count1 must > count2
LOGDIR="/var/log/outbound_ddos"
SECS=10
THRESHOLD1=65536000
COUNT1=10
THRESHOLD2=117964800
COUNT2=2

while [ 1 ]
do
    for veid in $(/usr/sbin/vzlist -o veid -H)
    do      
        # Create the log file if it doesn't already exist
        if ! test -e $LOGDIR/$veid.log; then
            touch $LOGDIR/$veid.log
        fi

        # Parse out the inbound/outbound traffic and assign them to the corresponding variables     
        eval $(/usr/sbin/vzctl exec $veid "grep venet0 /proc/net/dev"  |  
            awk -F: '{print $2}' | awk '{printf"CTOUT=%sn",$9}')

        # Print the output and a timestamp to a log file
        echo $(date +%s) $CTOUT >> $LOGDIR/$veid.log

        # Read last 10 entries into arrays
        i=0
        tail $LOGDIR/$veid.log | while read time byte
        do
            times[$i]=time; bytes[$i]=byte
            let i++
        done

        # Sanity checks and calculations
        for (( i=0; i<COUNT1; i++ ))
        do
            # Sanity check on the time
            if (( times[$i] > times[$i - 1] + $SECS + 10 ))
                then continue
            fi

            # Calculate differences
            let "diff[$i] = bytes[$i+1] - bytes[$i]"
        done

        # Work out thresholds and suspend where necessary
        # Higher threshold first
        t2=0
                for (( i=COUNT1-1; i>=0; i-- ))
                do
                        if [ ! diff[$i] ]
                                then continue
                                else
                                        if (( bytes[$i] < THRESHOLD1 * SECS + bytes[$i-1] + 1 ))
                                                then continue
                        else let t2++
                                        fi
                        fi
                done
        #Lower threshold last
        t1=0
        for (( i=COUNT1-1; i>=0; i-- ))
        do
            if [ ! diff[$i] ]
                then continue
                else
                    if (( bytes[$i] < THRESHOLD1 * SECS + bytes[$i-1] + 1 ))
                        then continue
                        else let t1++
                    fi
            fi
        done

        # Suspend where necessary
        if (( ($t2 >= $COUNT2) || ($t1 >= $COUNT1) ))
            then vzctl stop veid
        fi
    done
    sleep $SECS
done

而错误:

script.sh: line 38: times: bad array subscript
script.sh: line 54: bytes: bad array subscript
script.sh: line 67: bytes: bad array subscript

我已经尝试了数组下标的几种变体,但我似乎无法摆脱这些错误.

所以在第38行,
if (( times[$i] > times[$i - 1] + $SECS + 10 ))

在迭代期间会引用一次[-1]次.负指数是only very recently part of bash arrays,所以这很可能是你得到错误的原因.

与第54行和第67行类似,您只需按一次负数组下标.调整循环以避免[0 – 1].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读