website-debug/TestURL.sh

101 lines
3.6 KiB
Bash

#!/bin/bash
set -x # Enable debugging
# Check if the URL is passed as a parameter
if [ -z "$1" ]; then
echo "Please provide a URL as a parameter." >&2
exit 1
fi
url="$1"
hastebin_url="https://haste.nixc.us/documents"
ntfy_server="https://ntfy.nixc.us/bnHobG80sO5ZF4SB"
ntfy_credentials="admin:tiZcgc8Waf9mg5A4n2aJ4Qk2RyYnwEHDT"
temp_file=$(mktemp)
# Function to perform the URL and network tests
test_url() {
local url=$1
local result=""
# Get the current timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
result+="Timestamp: $timestamp, "
# Test HTTP status and response time
echo "Starting HTTP status check..." >&2
http_response=$(curl -o /dev/null -s -w "%{http_code} %{time_total}" "https://$url")
http_status=$(echo $http_response | awk '{print $1}')
http_time=$(echo $http_response | awk '{print $2}')
result+="Url: https://$url, StatusCode: $http_status, ResponseTime: $http_time, "
# Perform Ping test for IPv4 with timeout
echo "Starting Ping test for IPv4..." >&2
ping_ipv4=$(ping -c 4 -W 2 "$url" 2>&1)
if [[ $? -eq 0 ]]; then
avg_time=$(echo "$ping_ipv4" | grep 'round-trip' | awk -F'/' '{print $5}')
result+="PingIPv4: $avg_time ms avg, "
else
result+="PingIPv4: Ping (IPv4) failed: $(echo "$ping_ipv4" | tail -n 1 | xargs), "
fi
# Check for IPv6 availability and perform Ping test for IPv6 with timeout
echo "Checking IPv6 availability..." >&2
ping6 -c 1 -W 2 "$url" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "IPv6 is available, starting Ping test for IPv6..." >&2
ping_ipv6=$(ping6 -c 4 -W 2 "$url" 2>&1)
if [[ $? -eq 0 ]]; then
avg_time=$(echo "$ping_ipv6" | grep 'round-trip' | awk -F'/' '{print $5}')
result+="PingIPv6: $avg_time ms avg, "
else
result+="PingIPv6: Ping (IPv6) failed: $(echo "$ping_ipv6" | tail -n 1 | xargs), "
fi
else
result+="PingIPv6: IPv6 not available, "
fi
# Perform Traceroute with hop limit and timeout per hop
echo "Starting Traceroute..." >&2
traceroute_result=$(traceroute -m 15 -w 2 -n "$url" 2>&1)
result+="Traceroute: $(echo "$traceroute_result" | tr '\n' ' ' | tr -s ' '), "
# Perform NSLookup with various DNS servers
perform_nslookup() {
local dns_server=$1
nslookup_output=$(nslookup "$url" "$dns_server" 2>&1)
echo -e "$(echo "$nslookup_output" | grep -E '^Server:') $(echo "$nslookup_output" | grep -E 'Address:')"
}
echo "Starting NSLookup tests..." >&2
result+="NsLookupDefault: $(perform_nslookup), "
result+="NsLookupCloudflare: $(perform_nslookup 1.1.1.1), "
result+="NsLookupGoogle: $(perform_nslookup 8.8.8.8), "
result+="NsLookupIBM: $(perform_nslookup 9.9.9.9), "
echo "$result"
}
# Test the URL and save the result as a log
echo "Starting URL test..." >&2
log=$(test_url "$url")
echo "$log" > "$temp_file"
echo "URL test completed. Sending results to Hastebin..." >&2
# Send the log to Hastebin server
haste_key=$(curl -s -X POST -d @"$temp_file" "$hastebin_url" | jq -r '.key')
haste_link="https://haste.nixc.us/$haste_key"
echo "Results successfully sent to Hastebin." >&2
# Send a notification to ntfy server with the Hastebin link and test settings
echo "Sending notification to ntfy server..." >&2
ntfy_message=$(printf "URL test completed. Log available at: %s. Tested URL: %s" "$haste_link" "$url")
curl -u "$ntfy_credentials" -d "$ntfy_message" "$ntfy_server"
echo "Notification sent to ntfy." >&2
# Clean up the temporary file
rm "$temp_file"
set +x # Disable debugging