| 
									
										
										
										
											2023-05-07 20:23:09 +00:00
										 |  |  | #!/bin/bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Set the path to the log file | 
					
						
							| 
									
										
										
										
											2023-05-21 21:03:43 +00:00
										 |  |  | LOG_FILE="./speedtest.csv" | 
					
						
							| 
									
										
										
										
											2023-05-07 20:23:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 21:03:43 +00:00
										 |  |  | # Set your Discord webhook URL | 
					
						
							|  |  |  | WEBHOOK_URL="DISCORD_WEBHOOK_URL" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Set WAN Number | 
					
						
							|  |  |  | WAN="NUMBER" | 
					
						
							| 
									
										
										
										
											2023-05-07 20:23:09 +00:00
										 |  |  | # Run the speedtest and parse the output | 
					
						
							|  |  |  | SPEEDTEST=$(speedtest-cli --csv) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check if the output is empty or contains errors | 
					
						
							|  |  |  | if [ -z "$SPEEDTEST" ] || [[ "$SPEEDTEST" == *"ERROR"* ]]; then | 
					
						
							|  |  |  |   echo "Speedtest failed: $SPEEDTEST" | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PING=$(echo "$SPEEDTEST" | awk -F',' '{print $6}') | 
					
						
							|  |  |  | DOWNLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $7}') | 
					
						
							|  |  |  | UPLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $8}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check if the required fields are missing or invalid | 
					
						
							|  |  |  | if [ -z "$PING" ] || [ -z "$DOWNLOAD" ] || [ -z "$UPLOAD" ]; then | 
					
						
							|  |  |  |   echo "Missing or invalid speedtest fields: PING=$PING, DOWNLOAD=$DOWNLOAD, UPLOAD=$UPLOAD" | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Convert the download and upload speeds to Mbps | 
					
						
							|  |  |  | DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }") | 
					
						
							|  |  |  | UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Append the result to the log file | 
					
						
							|  |  |  | echo "$(date +"%Y-%m-%d %H:%M:%S"),$PING,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 21:03:43 +00:00
										 |  |  | # Prepare the payload for the Discord webhook | 
					
						
							|  |  |  | PAYLOAD='{"content": "Speedtest Result:\nPing: '"$PING"'\nDownload: '"$DOWNLOAD_Mbps"' Mbps\nUpload: '"$UPLOAD_Mbps"' Mbps"}' | 
					
						
							| 
									
										
										
										
											2023-05-07 20:23:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-21 21:03:43 +00:00
										 |  |  | # Send the payload to the Discord webhook - If you dont want this, comment out this command | 
					
						
							|  |  |  | curl -H "Content-Type: application/json" -X POST --data "{\"content\": null, \"embeds\": [{\"title\": \"Speedtest Result - WAN $WAN\", \"description\": \"Time: $(date +"%Y-%m-%d %H:%M:%S")\nPing: $PING ms\nDownload: $DOWNLOAD_Mbps mbps\nUpload: $UPLOAD_Mbps mbps\", \"color\": 5814783}], \"attachments\": []}" $WEBHOOK_URL |