Created
September 6, 2024 21:24
-
-
Save jranil/b06b87b30fe74b7605e457f878dba651 to your computer and use it in GitHub Desktop.
PowerShell script to check neshan.org connectivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test-Neshan.ps1 | |
# Function to get the public IP address | |
function Get-PublicIP { | |
try { | |
$ip = Invoke-RestMethod -Uri 'https://api.ipify.org' | |
return $ip | |
} catch { | |
return "Unable to determine" | |
} | |
} | |
# Function to get ASN information | |
function Get-ASNInfo { | |
param($IP) | |
try { | |
$asnInfo = Invoke-RestMethod -Uri "https://ipapi.co/$IP/json/" | |
return @{ | |
ASN = $asnInfo.asn | |
ISP = $asnInfo.org | |
} | |
} catch { | |
return @{ | |
ASN = "Unable to determine" | |
ISP = "Unable to determine" | |
} | |
} | |
} | |
# Main test function | |
function Test-Neshan { | |
$url = "https://neshan.org" | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$publicIP = Get-PublicIP | |
$asnInfo = Get-ASNInfo -IP $publicIP | |
try { | |
$response = Invoke-WebRequest -Uri $url -UseBasicParsing | |
$status = $response.StatusCode | |
$bodyLength = $response.Content.Length | |
$headers = $response.Headers | |
$accessible = $true | |
} catch { | |
$status = $_.Exception.Response.StatusCode.value__ | |
$bodyLength = 0 | |
$headers = @{} | |
$accessible = $false | |
} | |
$result = @{ | |
timestamp = $timestamp | |
url = $url | |
accessible = $accessible | |
status_code = $status | |
body_length = $bodyLength | |
headers = $headers | |
public_ip = $publicIP | |
asn = $asnInfo.ASN | |
isp = $asnInfo.ISP | |
} | |
return $result | |
} | |
# Function to generate HTML report | |
function Generate-HTMLReport { | |
param($testResult) | |
$accessStatus = if ($testResult.accessible) { "Accessible" } else { "Not Accessible" } | |
$accessColor = if ($testResult.accessible) { "green" } else { "red" } | |
$html = @" | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Neshan.org Access Test Report</title> | |
<style> | |
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; } | |
h1 { color: #2c3e50; } | |
.result { margin-bottom: 20px; } | |
.result h2 { color: #34495e; } | |
table { width: 100%; border-collapse: collapse; } | |
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } | |
th { background-color: #f2f2f2; } | |
</style> | |
</head> | |
<body> | |
<h1>Neshan.org Access Test Report</h1> | |
<div class="result"> | |
<h2>Test Results</h2> | |
<p><strong>Timestamp:</strong> $($testResult.timestamp)</p> | |
<p><strong>URL Tested:</strong> $($testResult.url)</p> | |
<p><strong>Access Status:</strong> <span style="color: $accessColor;">$accessStatus</span></p> | |
<p><strong>HTTP Status Code:</strong> $($testResult.status_code)</p> | |
<p><strong>Response Body Length:</strong> $($testResult.body_length) bytes</p> | |
</div> | |
<div class="result"> | |
<h2>Network Information</h2> | |
<p><strong>Public IP:</strong> $($testResult.public_ip)</p> | |
<p><strong>ASN:</strong> $($testResult.asn)</p> | |
<p><strong>ISP:</strong> $($testResult.isp)</p> | |
</div> | |
<div class="result"> | |
<h2>Response Headers</h2> | |
<table> | |
<tr> | |
<th>Header</th> | |
<th>Value</th> | |
</tr> | |
$(foreach ($header in $testResult.headers.GetEnumerator()) { | |
"<tr><td>$($header.Key)</td><td>$($header.Value)</td></tr>" | |
}) | |
</table> | |
</div> | |
</body> | |
</html> | |
"@ | |
return $html | |
} | |
# Run the test | |
$testResult = Test-Neshan | |
# Generate HTML report | |
$htmlReport = Generate-HTMLReport -testResult $testResult | |
# Save the HTML report | |
$reportPath = Join-Path $env:USERPROFILE "Desktop\neshan_test_report.html" | |
$htmlReport | Out-File -FilePath $reportPath -Encoding utf8 | |
Write-Host "Test completed. HTML report saved to: $reportPath" | |
# Open the report in the default browser | |
Start-Process $reportPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment