Skip to content

Instantly share code, notes, and snippets.

@Lincest
Last active October 19, 2023 16:29
Show Gist options
  • Save Lincest/e360c004b298ca5e6695c736bca81479 to your computer and use it in GitHub Desktop.
Save Lincest/e360c004b298ca5e6695c736bca81479 to your computer and use it in GitHub Desktop.
aws lightsail 流量统计和自动关机脚本
#!/bin/bash
# 需要提前安装: vnstat(流量统计工具), bc(计算器), telegram-cli(可删除或替换成其他通知工具)
# 流量限制
limit=900 # GB
# 获取出站和入站流量之和(以GB为单位)
traffic=$(vnstat --oneline b | awk -F';' '{print $11}')
# 将流量转换为GB
traffic_gb=$(echo "scale=2; $traffic / 1024 / 1024 / 1024" | bc)
# 展示当前流量
echo "lightsail本月流量: $traffic_gb GB, 流量限制: $limit GB" | telegram -
# 判断流量是否超过900GB
if (( $(echo "$traffic_gb > $limit" | bc -l) )); then
echo "lightsail 月流量超过 $limit GB,将自动关机!" | telegram -
shutdown -h now
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment