Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active February 19, 2023 05:28
Show Gist options
  • Save gilangvperdana/4eb37653b18ea77d543a170c6c75b4b9 to your computer and use it in GitHub Desktop.
Save gilangvperdana/4eb37653b18ea77d543a170c6c75b4b9 to your computer and use it in GitHub Desktop.
Alert Internet Speed Periodicly with Speedtest CLI on Ubuntu Server

Goals

  • Send periodicly an Internet speed with speedtest-cli to group telegram with Telgeram BOT

Prerequisites

sudo apt install speedtest-cli

Script

#!/bin/bash
PATH=/usr/local/bin:/bin:/usr/bin

# Token bot Telegram Anda
BOT_TOKEN="BOT_TOKEN"
# ID grup Telegram yang akan menerima hasil tes kecepatan internet
GROUP_ID="CHAT_ID"

# Fungsi untuk mengirim hasil tes kecepatan internet ke grup Telegram
function send_to_telegram {
    message=$1
    curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" -d chat_id=$GROUP_ID -d text="$message" -d parse_mode="HTML"
}

# Tes kecepatan internet menggunakan speedtest-cli
result=$(speedtest-cli --simple)

# Waktu dan tanggal tes
timestamp=$(date "+%Y-%m-%d %H:%M:%S")

# Pesan untuk dikirim ke Telegram
message="<b>Laporan kecepatan internet $timestamp pada Server Lenovo</b>%0A%0A<b>Ping:</b> $(echo "$result" | awk '/Ping/ {print $2}')%0A<b>Download:</b> $(echo "$result" | awk '/Download/ {print $2 " " $3}')%0A<b>Upload:</b> $(echo "$result" | awk '/Upload/ {print $2 " " $3}')"

# Kirim pesan ke Telegram
send_to_telegram "$message

Alert Periodicly

To alert periodicly we can use cronjob to automate execute this script, you can use Crontab Guru to design your automation day or time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment