首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash计算PPS

Bash计算PPS
EN

Unix & Linux用户
提问于 2022-08-27 13:50:37
回答 2查看 94关注 0票数 0

我做了一个小bash脚本,通过它可以计算来自接口的pps。一旦传入的pps达到所需的限制,它就会执行一个命令。

我在运行脚本时出错了,有人能帮我吗?

这是剧本

代码语言:javascript
复制
#!/bin/bash

INTERVAL="1"  # update interval in seconds
LIMIT="3000" # Limit in KB/S
URL1="http://1.1.1.1/abcd.php"
IFS=( ens3 ) # Interface names

while true
do
        for i in "${IFS[@]}"
        do
            R1=$(cat /sys/class/net/$i/statistics/rx_packets)
            T1=$(cat /sys/class/net/$i/statistics/tx_packets)
            sleep $INTERVAL
            R2=$(cat /sys/class/net/$i/statistics/rx_packets)
            T2=$(cat /sys/class/net/$i/statistics/tx_packets)
            TBPS=$(expr $T2 - $T1)
            RBPS=$(expr $R2 - $R1)

            echo "Incoming $i: $RKBPS pps || Outgoing $i: $TKBPS pps"
            if (( $RKBPS > $LIMIT )); then
                # Incoming Limit Exceeded
                #bash $URL1
                #sleep 10
                curl $URL1
                sleep 320
            fi
        done
done

错误如下所示

代码语言:javascript
复制
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")
Incoming ens3:  pps || Outgoing ens3:  pps
./s.sh: line 22: ((: > 3000 : syntax error: operand expected (error token is "> 3000 ")

有人能帮帮我吗。T.I.A

EN

回答 2

Unix & Linux用户

发布于 2022-08-27 13:56:54

您正在设置两个变量TBPSRBPS,然后引用TKBPSRKBPS

您还应该在sleep语句之外添加一个简短的if,否则它将消耗大量的CPU,因为在没有超出值的情况下,它将处于一个紧密的循环中。

票数 1
EN

Unix & Linux用户

发布于 2022-08-27 13:57:32

if而不是这个:

代码语言:javascript
复制
 if (( $RKBPS > $LIMIT )); then

应:

代码语言:javascript
复制
 if [ "$RKBPS" -gt "$LIMIT" ]; then

这是很奇怪的方法来塑造交通。也许你可以用软件来实现一些流量整形器。

另外,您从rx_packets获得的变量大约是每秒的数据包,而不是每秒的字节。你应该使用rx_bytes

您还忘记在if之前添加此内容(以千字节为单位转换字节):

代码语言:javascript
复制
TKBPS=$(expr $TBPS / 1024)
RKBPS=$(expr $RBPS / 1024) 
票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/715108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档