Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Created May 21, 2024 00:54
Show Gist options
  • Save nanpuyue/47a56ae14fb107f897a1beccb4ba9f1c to your computer and use it in GitHub Desktop.
Save nanpuyue/47a56ae14fb107f897a1beccb4ba9f1c to your computer and use it in GitHub Desktop.
Set the Interface Down if Monthly Traffic Limit Exceeded
#!/bin/bash
# date: 2024-05-21
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# author: nanpuyue <[email protected]> https://blog.nanpuyue.com
# 设置接口名称和流量限制
IFACE="eth0"
LIMIT="190 GiB"
# vnstat 数据库文件路径
VNSTAT_DB="/var/lib/vnstat/vnstat.db"
while read line; do
[ "$line" = MODIFY ] || continue
# 使用 vnstat 检查接口流量
if ! vnstat -i $IFACE --alert 0 3 m tx $LIMIT; then
echo "Monthly transmitted data limit exceeded. Set $IFACE down."
# 将接口设为 down
ip link set $IFACE down
break
fi
# 使用 inotifywait 监控 vnstat 数据库文件的修改
done < <(inotifywait -qme modify --format %e $VNSTAT_DB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment