Lentin

一个流量超出导致的惨案,搓了个流量超出后停服不关机的脚本

  •  
  •   Lentin · 7 days ago · 1537 views

    安装依赖

    apt install vnstat
    

    crontab

    * * * * * vnstat -i ens5 --alert 0 3 m total 1800 GB || /bin/bash /home/admin/stats.sh
    

    1800GB 改为你想指定的流量上限 /home/admin/stats.sh

    #!/bin/bash
    
    # 核心服务名称
    SERVICE_NAME="xray"
    # 你的 Bark 密钥
    BARK_KEY=""
    
    # 1. 核心防御:检查 xray 服务是否仍在运行
    # 如果已经关闭,则默默退出,不重复操作
    if ! systemctl is-active --quiet "$SERVICE_NAME"; then
        exit 0
    fi
    
    # 2. 如果服务在运行,说明是第一次到达阈值,打印日志
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] vnStat 检测到月流量超标,触发熔断..." >> /home/admin/stats.log
    
    # 3. 发送 Bark 推送
    curl -X POST "https://api.day.app/$BARK_KEY" \
         -d "level=critical" \
         -d "volume=5" \
         -d "title=🚨 Lightsail 流量熔断警告" \
         -d "body=本月流量已超过 1800 GB 限制,已自动关闭服务!" \
         > /dev/null 2>&1
    
    # 4. 停止代理服务
    systemctl stop "$SERVICE_NAME"
    

    当然 也可以自动关机,发送通知后直接 shutdown now 就可以了

    3 replies    2026-07-06 14:12:07 +08:00
    datou
        1
    datou  
       6 days ago
    支持 lightsail 吗?
    Lentin
        2
    Lentin  
    OP
       6 days ago
    @datou 通用的,我的就是 lightsail ,你注意一下网卡名也修改一下
    kisstrnt
        3
    kisstrnt  
       5 days ago
    支持
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1030 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 18:56 · PVG 02:56 · LAX 11:56 · JFK 14:56
    ♥ Do have faith in what you're doing.