-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtestdiscord.sh
More file actions
81 lines (69 loc) · 2.3 KB
/
speedtestdiscord.sh
File metadata and controls
81 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
# --- SETTINGS ---
WEBHOOK_URL="INSERT DISCORD WEBHOOK_URL HERE"
FRIENDLY_NAME="INSERT DISCORD POST SPEEDTEST TITLE HERE"
DISCORD_USER="INSERT DISCORD POST DISPLAY USERNAME HERE"
LOG_FILE="/root/speedtest.log"
# ----------------------
DATE=$(date "+%Y-%m-%d %H:%M")
# Run Speedtest-go (Single Thread, ICMP Ping)
RAW_JSON=$(/usr/bin/speedtest-go --thread 1 --ping-mode=icmp --json 2>&1)
if echo "$RAW_JSON" | grep -q "dl_speed"; then
# 1. PARSING: Extract and convert all data using JQ
PARSED=$(echo "$RAW_JSON" | jq -r '
.servers[0] |
[
(.dl_speed / 125000),
(.ul_speed / 125000),
(.latency / 1000000),
(.min_latency / 1000000),
(.max_latency / 1000000),
(.jitter / 1000000),
.sponsor,
.distance,
.packet_loss.sent,
.packet_loss.max
] | join("|")')
# 2. ASSIGN: Split values into variables
IFS="|" read -r DL UL PNG PMIN PMAX JIT SRV DST SENT MAX <<EOF
$PARSED
EOF
# 3. FORMAT: Rounding
DL_MBPS=$(printf "%.2f" $DL)
UL_MBPS=$(printf "%.2f" $UL)
PNG_MS=$(printf "%.2f" $PNG)
PMIN_MS=$(printf "%.2f" $PMIN)
PMAX_MS=$(printf "%.2f" $PMAX)
JIT_MS=$(printf "%.2f" $JIT)
DST_KM=$(printf "%.1f" $DST)
# 4. PACKET LOSS: Your verified logic (diff = sent - max)
LOSS_VAL=$(awk -v s="$SENT" -v m="$MAX" 'BEGIN {
diff = s - m;
if (diff <= 1 || s == 0) {
print "0.00"
} else {
printf "%.2f", (diff * 100) / s
}
}')
# 5. CONTENT: Build the Discord post
CONTENT="**$FRIENDLY_NAME**
$DATE
Server: $SRV ($DST_KM km)
\`\`\`
Ping: $PNG_MS ms [$PMIN_MS-$PMAX_MS]
Download: $DL_MBPS Mbps
Upload: $UL_MBPS Mbps
Jitter: $JIT_MS ms
Loss: $LOSS_VAL%
\`\`\`"
# Log to file
echo "$DATE | Ping: $PNG_MS ms Download: $DL_MBPS Mbps Upload: $UL_MBPS Mbps" >> "$LOG_FILE"
else
CONTENT="**$FRIENDLY_NAME - ERROR**
$DATE
Speedtest failed."
echo "$DATE | Speedtest Failed" >> "$LOG_FILE"
fi
# 6. SEND: Post to Discord using a safe JQ-generated payload
PAYLOAD=$(jq -n --arg user "$DISCORD_USER" --arg cont "$CONTENT" '{username: $user, content: $cont}')
/usr/bin/curl -s -H "Content-Type: application/json" -X POST -d "$PAYLOAD" "$WEBHOOK_URL" > /dev/null