From f9319e6ce652d9446c42f45f1954bd20ae18a8df Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 27 Aug 2024 09:09:12 -0400 Subject: [PATCH] Website connectivity debugger --- TESTURL.md | 24 +++++++++ TestURL.ps1 | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 TESTURL.md create mode 100644 TestURL.ps1 diff --git a/TESTURL.md b/TESTURL.md new file mode 100644 index 0000000..d9d89b4 --- /dev/null +++ b/TESTURL.md @@ -0,0 +1,24 @@ +# PowerShell Script for URL and Network Testing + +This PowerShell script performs comprehensive network tests for a specified URL, including HTTP status checks, ping tests (IPv4 and IPv6), traceroute, and NSLookup. The results are automatically sent to a Hastebin server for logging, and a notification is pushed to a configured ntfy server. + +## How to Use the Script + +1. **Download and Save the Script**: Save the provided PowerShell script to a file on your computer, such as `TestUrl.ps1`. + +2. **Open PowerShell**: Run PowerShell with the necessary permissions to execute scripts. + +3. **Run the Script with a URL Argument**: Use the following command to run the script, replacing `example.com` with the target URL you wish to test: + + ```powershell + .\TestUrl.ps1 -url "example.com" + ``` + + Make sure to provide the URL without the `https://` prefix when performing network tests like ping and traceroute. + +4. **View the Results**: The script will perform several network tests on the specified URL, send the logs to your Hastebin server, and push a notification to your ntfy server with a link to the log and test details. + +## Requirements + +- This script uses only default Windows tools (e.g., `Test-Connection`, `tracert`, `nslookup`) and does not require any additional installations. +- Make sure your network settings allow the use of PowerShell for sending HTTP requests and running network commands like ping and traceroute. \ No newline at end of file diff --git a/TestURL.ps1 b/TestURL.ps1 new file mode 100644 index 0000000..b208df4 --- /dev/null +++ b/TestURL.ps1 @@ -0,0 +1,149 @@ +# Check if the URL is passed as a parameter +param ( + [string]$url +) + +if (-not $url) { + Write-Error "Please provide a URL as a parameter." + exit 1 +} + +# Define the servers to send logs and notifications +$hastebinUrl = "https://haste.nixc.us/documents" +$ntfyServer = "https://ntfy.nixc.us/bnHobG80sO5ZF4SB" +$ntfyCredentials = "admin:tiZcgc8Waf9mg5A4n2aJ4Qk2RyYnwEHDT" + +# Function to perform the URL and network tests +function Test-Url { + param ($url) + $result = @{ + Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") # Explicitly convert to string format to avoid epoch time + Url = $url.Trim() + StatusCode = "" + ResponseTime = "" + Error = "" + PingIPv4 = "" + PingIPv6 = "" + Traceroute = "" + NsLookupDefault = "" + NsLookupCloudflare = "" + NsLookupGoogle = "" + NsLookupIBM = "" + NameserverDefault = "" + } + + # Test HTTP status and response time + Write-Host "Starting HTTP status check..." + try { + $startTime = Get-Date + $response = Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 10 + $endTime = Get-Date + $result.StatusCode = $response.StatusCode + $result.ResponseTime = [math]::Round(($endTime - $startTime).TotalMilliseconds, 2) # Time in milliseconds + Write-Host "HTTP status check completed." + } catch { + $result.Error = $_.Exception.Message.Trim() + Write-Host "HTTP status check failed: $($_.Exception.Message)" + } + + # Perform Ping test for IPv4 + Write-Host "Starting Ping test for IPv4..." + try { + $pingIPv4 = Test-Connection -ComputerName $url -Count 4 -ErrorAction Stop + $result.PingIPv4 = ($pingIPv4 | Select-Object -First 1).Address.ToString().Trim() + ": " + [math]::Round(($pingIPv4 | Measure-Object ResponseTime -Average).Average, 2) + " ms avg" + Write-Host "Ping test for IPv4 completed." + } catch { + $result.PingIPv4 = "Ping (IPv4) failed: " + $_.Exception.Message.Trim() + Write-Host "Ping test for IPv4 failed: $($_.Exception.Message)" + } + + # Check if IPv6 is available and perform Ping test for IPv6 + Write-Host "Checking IPv6 availability..." + try { + $pingIPv6Available = Test-Connection -ComputerName $url -Count 1 -ErrorAction Stop -IPv6 + Write-Host "IPv6 is available, starting Ping test for IPv6..." + try { + $pingIPv6 = Test-Connection -ComputerName $url -Count 4 -ErrorAction Stop -IPv6 + $result.PingIPv6 = ($pingIPv6 | Select-Object -First 1).Address.ToString().Trim() + ": " + [math]::Round(($pingIPv6 | Measure-Object ResponseTime -Average).Average, 2) + " ms avg" + Write-Host "Ping test for IPv6 completed." + } catch { + $result.PingIPv6 = "Ping (IPv6) failed: " + $_.Exception.Message.Trim() + Write-Host "Ping test for IPv6 failed: $($_.Exception.Message)" + } + } catch { + $result.PingIPv6 = "IPv6 not available" + Write-Host "IPv6 is not available." + } + + # Perform Traceroute + Write-Host "Starting Traceroute..." + try { + $traceroute = tracert.exe -d $url 2>&1 | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" } + $result.Traceroute = ($traceroute -join " ") -replace '\s+', ' ' + Write-Host "Traceroute completed." + } catch { + $result.Traceroute = "Traceroute failed: " + $_.Exception.Message.Trim() + Write-Host "Traceroute failed: $($_.Exception.Message)" + } + + # Function to perform nslookup with a specific DNS server + function Perform-NsLookup { + param ($dnsServer, $label) + try { + $outputFile = "$label.txt" + $process = Start-Process nslookup.exe -ArgumentList "$url $dnsServer" -NoNewWindow -RedirectStandardOutput $outputFile -PassThru + $process.WaitForExit() + $nslookup = Get-Content $outputFile | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" } + + # Extract the nameserver information if using the default + if ($label -eq "Default") { + $serverLine = $nslookup | Select-String -Pattern '^Server:' + $addressLine = $nslookup | Select-String -Pattern '^Address:' + $result.NameserverDefault = "Server: $($serverLine -replace 'Server:\s*','') Address: $($addressLine -replace 'Address:\s*','')" + } + + $result["NsLookup$label"] = ($nslookup -join " ") -replace '\s+', ' ' + Remove-Item $outputFile -Force -ErrorAction SilentlyContinue # Clean up output file + Write-Host "NSLookup ($label) completed." + } catch { + $result["NsLookup$label"] = "NSLookup ($label) failed: " + $_.Exception.Message.Trim() + Write-Host "NSLookup ($label) failed: $($_.Exception.Message)" + } + } + + # Perform nslookup with default DNS, Cloudflare, Google, and IBM DNS servers + Perform-NsLookup "Default" + Perform-NsLookup "1.1.1.1" "Cloudflare" + Perform-NsLookup "8.8.8.8" "Google" + Perform-NsLookup "9.9.9.9" "IBM" + + return $result +} + +# Test the URL and save the result as a log +Write-Host "Starting URL test..." +$log = Test-Url -url $url | ConvertTo-Json -Depth 10 +Write-Host "URL test completed. Sending results to Hastebin..." + +# Send the log to Hastebin server +try { + $prettyLog = $log -replace '\n', ' ' # Replace newline characters with spaces + $hasteResponse = Invoke-RestMethod -Uri $hastebinUrl -Method Post -Body $prettyLog -ContentType "application/json" + $hasteKey = $hasteResponse.key + $hasteLink = "https://haste.nixc.us/" + $hasteKey + Write-Host "Results successfully sent to Hastebin." +} catch { + Write-Error "Failed to send log to Hastebin: $_" + exit 1 +} + +# Send a notification to ntfy server with the Hastebin link and test settings +Write-Host "Sending notification to ntfy server..." +try { + $ntfyMessage = "URL test completed. Log available at: $hasteLink`nTested URL: $url" + $ntfyResponse = Invoke-RestMethod -Uri $ntfyServer -Method Post -Body $ntfyMessage -Headers @{ Authorization = "Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($ntfyCredentials)))" } + Write-Host "Notification sent to ntfy." +} catch { + Write-Error "Failed to send notification to ntfy: $_" + exit 1 +}