我做了一个小bash脚本,通过它可以计算来自接口的pps。一旦传入的pps达到所需的限制,它就会执行一个命令。
我在运行脚本时出错了,有人能帮我吗?
这是剧本
#!/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错误如下所示
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
发布于 2022-08-27 13:56:54
您正在设置两个变量TBPS和RBPS,然后引用TKBPS和RKBPS。
您还应该在sleep语句之外添加一个简短的if,否则它将消耗大量的CPU,因为在没有超出值的情况下,它将处于一个紧密的循环中。
发布于 2022-08-27 13:57:32
if而不是这个:
if (( $RKBPS > $LIMIT )); then应:
if [ "$RKBPS" -gt "$LIMIT" ]; then这是很奇怪的方法来塑造交通。也许你可以用软件来实现一些流量整形器。
另外,您从rx_packets获得的变量大约是每秒的数据包,而不是每秒的字节。你应该使用rx_bytes
您还忘记在if之前添加此内容(以千字节为单位转换字节):
TKBPS=$(expr $TBPS / 1024)
RKBPS=$(expr $RBPS / 1024) https://unix.stackexchange.com/questions/715108
复制相似问题