代码之家  ›  专栏  ›  技术社区  ›  Goeks

Bash while do and if with Telegram API

  •  0
  • Goeks  · 技术社区  · 6 年前

    我正在尝试全天候监控流。 为此,我监视我的树莓的网络速度。

    网络速度(KB):

    (old="$(</sys/class/net/eth0/statistics/rx_bytes)"; while $(sleep 1); do  now=$(</sys/class/net/eth0/statistics/rx_bytes); echo $((($now-$old)/1024)); old=$now; done)
    

    输出单位为Kbit/s:

        216
        384
        288
        360
        336
        360
    

    电报代码:

    TELEGRAM=$(curl -s -X POST 'https://api.telegram.org/<BOT ID>:<API KEY>/sendMessage?chat_id=<ID>&text="Stream has problems"')
    

    为此,我需要一个whiledo循环和if条件。

    编辑////

    $(old="$(</sys/class/net/eth0/statistics/rx_bytes)"; while $(sleep 1); do  now=$(</sys/class/net/eth0/statistics/rx_bytes); if (( (now-old)/1024 < 1000 )); then $TELEGRAM ; fi echo $((($now-$old)/1024)); old=$now; done)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Goeks    6 年前

    我已经解决了。解决方案如下。

    #!/bin/bash
    
    OLD="$(</sys/class/net/eth0/statistics/rx_bytes)"
    i=1
            while $(sleep 1m); do
    NOW=$(</sys/class/net/eth0/statistics/rx_bytes)
    
    if (( (NOW-OLD)/1024 > 100 )) ; then
    
            remainder=$(( i % 3 ))
            [ "$remainder" -eq 0 ] && curl -s -X POST 'https://api.telegram.org/<BOT ID>:<API KEY>/sendMessage?chat_id=<ID>&text="Stream has problems"'
            i=$(( i + 1 ))
    fi
    OLD=$NOW
    done