跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 写了个 shell 脚本笑不活了

发表于

之前不是发了个帖子想低价嫖阿里的 5 年 200G 免费流量看看外面的世界嘛,还询问怎么防止盗刷流量

https://v2ex.com/t/1053353

然后我自己糊了一个,因为是测测试脚本流量上限不高只有 150G 并且是上下行相加的。

我怕被刷 crontab 设置的比较急进 1 分钟

今天绝了开机就关开机就关,手速跟不上了。。。

有什么好方法能让我上机一手不我去改改脚本

emmm

#!/bin/bash

# 设置网卡名称
INTERFACE="eth0"
# 设置流量限制(单位:GB )
LIMIT=150

# 检查 vnstat 和 jq 是否已安装
if ! command -v vnstat &> /dev/null; then
echo "vnstat 未安装,请安装后重试。"
exit 1
fi

if ! command -v jq &> /dev/null; then
echo "jq 未安装,请安装后重试。"
exit 1
fi

# 检查 bc 是否已安装
if ! command -v bc &> /dev/null; then
echo "bc 未安装,请安装后重试。"
exit 1
fi

# 获取当前流量(单位:KB )
VNSTAT_JSON=$(vnstat -i $INTERFACE --json)
echo "vnstat JSON 输出: $VNSTAT_JSON"

# 使用 jq 解析 JSON 数据获取接收和发送的流量(单位:KB )
RX=$(echo $VNSTAT_JSON | jq -r '.interfaces[0].traffic.total.rx')
TX=$(echo $VNSTAT_JSON | jq -r '.interfaces[0].traffic.total.tx')

# 输出解析结果
echo "接收流量 (RX): $RX KB"
echo "发送流量 (TX): $TX KB"

# 检查 RX 和 TX 是否为有效的数字
if ! [[ $RX =~ ^[0-9]+$ ]] || ! [[ $TX =~ ^[0-9]+$ ]]; then
echo "RX 或 TX 不是有效的数字。"
exit 1
fi

# 计算总流量(单位:GB )
TOTAL=$(echo "scale=2; ($RX + $TX) / 1024 / 1024 / 1024" | bc)

# 输出当前总流量
echo "当前流量:$TOTAL GB"

# 检查是否超过流量限制
if (( $(echo "$TOTAL >= $LIMIT" | bc -l) )); then
echo "流量限制已超出,正在禁用网络接口。"
sudo shutdown now
else
echo "当前流量未超出限制。"
fi
#!/bin/bash

# 停止 vnStat 服务
sudo systemctl stop vnstat # 如果使用 systemd 管理服务

# 删除 vnStat 数据库文件(根据需要修改网络接口名称)
sudo rm -f /var/lib/vnstat/* # 删除所有 vnstat 数据库文件

# 重新启动 vnStat 服务
sudo systemctl start vnstat # 如果使用 systemd 管理服务

echo "vnStat 流量统计数据已重置。"

# 每 5 分钟检查一次流量
*/5 * * * * /root/check_traffic.sh

# 每月 1 号重置网络和流量统计
0 0 1 * * /root/reset_network.sh

Featured Replies

No posts to show

创建帐户或登录来提出意见

Account

导航

搜索

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.