一元网络论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 109|回复: 0

完善了MJJ的阿里云3年白嫖脚本

[复制链接]

1万

主题

1万

帖子

4万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
49933
发表于 2024-8-1 18:37:13 | 显示全部楼层 |阅读模式
```bash
#!/usr/bin/env bash
# This script alerts on SSH login via /etc/profile.d/sshd_telegram.sh and monitors bandwidth usage.
# Adapted from: https://github.com/MyTheValentinus/ssh-login-alert-telegram
# Define your USERID (Telegram chat ID), KEY (Telegram bot API key), VPSNAME (server name), PFTIME (interval in seconds), LIMIT and LIMIT2 (bandwidth thresholds in GB).
USERID="Your_Telegram_ID_here"
KEY="Your_Bot_Key_Here"
VPSNAME="ali-hk1"
LIMIT=150
LIMIT2=160
PFTIME=1800
# Loop through USERIDs
for i in $USERID
do
    URL="https://api.telegram.org/bot$KEY/sendMessage"
    DATE="$(date "+%Y-%m-%d %H:%M:%S")"
    INTERFACE="eth0"
    SRV_HOSTNAME=$(hostname -f)
    # Fetch current traffic data in KB
    VNSTAT_JSON=$(vnstat -i $INTERFACE --json)
    RX=$(echo $VNSTAT_JSON | jq -r '.interfaces[0].traffic.total.rx')
    TX=$(echo $VNSTAT_JSON | jq -r '.interfaces[0].traffic.total.tx')
    # Ensure RX and TX are valid numbers
    if ! [[ $RX =~ ^[0-9]+$ ]] || ! [[ $TX =~ ^[0-9]+$ ]]; then
        exit 1
    fi
    # Calculate total traffic in GB
    TOTAL=$(echo "scale=2; ($RX + $TX) / 1024 / 1024" | bc)
    RX_GB=$(echo "scale=2; $RX / 1024 / 1024" | bc)
    TX_GB=$(echo "scale=2; $TX / 1024 / 1024" | bc)
    # Timestamp management
    current_time=$(date +%s)
    timestamp_file="/usr/unitls/last_exec_time.txt"
   
    if [ -f "$timestamp_file" ]; then
        last_exec_time=$(cat "$timestamp_file")
    else
        touch $timestamp_file
        echo "Timestamp file created."
    fi
    time_diff=$((current_time - last_exec_time))
    # Check and act based on traffic
    if (( $(echo "$RX_GB >= $LIMIT2" | bc -l) )) || (( $(echo "$TX_GB >= $LIMIT2" | bc -l) )); then
        TEXT="${VPSNAME}(${SRV_HOSTNAME}) Traffic Usage:\nInbound: *$RX_GB*\nOutbound: *$TX_GB*\nTime: $DATE\nExceeded 160GB, shutting down."
        curl -s -d "chat_id=$i&text=$TEXT&disable_web_page_preview=true&parse_mode=markdown" $URL > /dev/null
        sudo shutdown -h now
    elif (( $(echo "$RX_GB >= $LIMIT" | bc -l) )) || (( $(echo "$TX_GB >= $LIMIT" | bc -l) )); then
        TEXT="${VPSNAME}(${SRV_HOSTNAME}) Traffic Usage:\nInbound: *$RX_GB*\nOutbound: *$TX_GB*\nTime: $DATE\nExceeded 150GB, nearing 160GB."
        curl -s -d "chat_id=$i&text=$TEXT&disable_web_page_preview=true&parse_mode=markdown" $URL > /dev/null
    else
        if (( time_diff >= PFTIME )); then
            echo "$current_time" > "$timestamp_file"
            TEXT="${VPSNAME}(${SRV_HOSTNAME}) Traffic Usage:\nInbound: *$RX_GB*\nOutbound: *$TX_GB*\nTime: $DATE\nUsage within limits."
            curl -s -d "chat_id=$i&text=$TEXT&disable_web_page_preview=true&parse_mode=markdown" $URL > /dev/null
        fi
    fi
done
# The system will shut down if either inbound or outbound traffic exceeds 160GB, sending a notification to the Telegram bot. The script runs every minute, checking for overages and sending updates every 30 minutes if no overage has occurred.
# Dependencies: vnstat, jq, and bc must be installed. vnstat is configured to record data every minute.
```
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|一元网络论坛

GMT+8, 2024-9-10 09:50 , Processed in 0.131123 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表