| 
 | 
 
```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. 
``` |   
 
 
 
 |