Última actividad 1756389216

elicro revisó este gist 1756389216. Ir a la revisión

1 file changed, 164 insertions

test-cloud-local-server.ps1(archivo creado)

@@ -0,0 +1,164 @@
1 + param(
2 + [Parameter(Mandatory=$true)]
3 + [string]$HostName
4 + )
5 +
6 + # Script version for verification
7 + $ScriptVersion = "1.0.7"
8 + Write-Host "Running test-cloud-local-server.ps1 version $ScriptVersion" -ForegroundColor Cyan
9 +
10 + # Trim whitespace and validate hostname
11 + $HostName = $HostName.Trim()
12 + if ([string]::IsNullOrWhiteSpace($HostName)) {
13 + Write-Host "Error: Hostname is missing or invalid." -ForegroundColor Red
14 + Write-Host "Usage: .\test-cloud-local-server.ps1 -HostName <IP_or_Hostname>" -ForegroundColor Yellow
15 + Write-Host "Example: .\test-cloud-local-server.ps1 -HostName 1.1.1.1" -ForegroundColor Yellow
16 + Write-Host "Description: This script checks your public IP and tests connections on ports 2222 (SSH), 27017 (MongoDB, HTTP 200/404 with body), and 8020 (web service, HTTP 200/404) of the specified host." -ForegroundColor Yellow
17 + exit 1
18 + }
19 +
20 + # Validate hostname as IP or domain
21 + if (-not ($HostName -match "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$|^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")) {
22 + Write-Host "Error: '$HostName' is not a valid IP address or hostname." -ForegroundColor Red
23 + Write-Host "Usage: .\test-cloud-local-servert.ps1 -HostName <IP_or_Hostname>" -ForegroundColor Yellow
24 + Write-Host "Example: .\test-cloud-local-servert.ps1 -HostName 1.1.1.1" -ForegroundColor Yellow
25 + exit 1
26 + }
27 +
28 + # Debug: Output the hostname to confirm it’s being received
29 + Write-Host "Checking ports on host: $HostName" -ForegroundColor Cyan
30 +
31 + # Check public IP from multiple services
32 + $services = @{
33 + "ipify" = @{Url = "https://api.ipify.org?format=json"; Field = "ip"}
34 + "ipapi" = @{Url = "https://ipapi.co/json/"; Field = "ip"}
35 + "jsonip" = @{Url = "https://jsonip.com"; Field = "ip"}
36 + }
37 +
38 + foreach ($service in $services.Keys) {
39 + try {
40 + $response = Invoke-WebRequest -Uri $services[$service].Url -Method Get -UseBasicParsing -TimeoutSec 5
41 + $json = $response.Content | ConvertFrom-Json
42 + $ip = $json.($services[$service].Field)
43 + Write-Host "Public IP from ${service}: $ip" -ForegroundColor Green
44 + } catch {
45 + Write-Host "Failed to get public IP from ${service}: Catch - $($_.Exception.Message)" -ForegroundColor Red
46 + }
47 + }
48 +
49 + # Port 2222: Check for SSH using raw TCP connection
50 + try {
51 + Write-Host "Attempting to connect to ${HostName}:2222" -ForegroundColor Cyan
52 + $tcpClient = New-Object System.Net.Sockets.TcpClient
53 + $connectTask = $tcpClient.ConnectAsync($HostName, 2222)
54 + $wait = $connectTask.Wait(5000) # 5-second timeout
55 + if ($wait -and $tcpClient.Connected) {
56 + $stream = $tcpClient.GetStream()
57 + $reader = New-Object System.IO.StreamReader($stream)
58 + $banner = $reader.ReadLine()
59 + $tcpClient.Close()
60 + if ($banner -like "SSH-2.0-*") {
61 + Write-Host "Port 2222: Success - SSH service detected ($banner)" -ForegroundColor Green
62 + Write-Host "OK" -ForegroundColor Green
63 + } else {
64 + # Try HTTP as a fallback
65 + try {
66 + $response = Invoke-WebRequest -Uri "http://$($HostName):2222/" -Method Get -UseBasicParsing -TimeoutSec 5 -ErrorAction SilentlyContinue
67 + if ($response -and ($response.StatusCode -eq 200 -or $response.StatusCode -eq 404)) {
68 + Write-Host "Port 2222: HTTP Success - Status $($response.StatusCode)" -ForegroundColor Green
69 + Write-Host "OK" -ForegroundColor Green
70 + } else {
71 + Write-Host "Port 2222: Failure - Unexpected HTTP status $(if ($response) { $response.StatusCode } else { 'No response' })" -ForegroundColor Red
72 + Write-Host "FAILED" -ForegroundColor Red
73 + }
74 + } catch {
75 + Write-Host "Port 2222: Failure - Non-SSH, non-HTTP service detected: Catch - $($_.Exception.Message)" -ForegroundColor Red
76 + Write-Host "FAILED" -ForegroundColor Red
77 + }
78 + }
79 + } else {
80 + Write-Host "Port 2222: Failure - Port is closed or unreachable: Catch - Connection failed" -ForegroundColor Red
81 + Write-Host "FAILED" -ForegroundColor Red
82 + if ($tcpClient) { $tcpClient.Close() }
83 + }
84 + } catch {
85 + Write-Host "Port 2222: Failure - Catch - $($_.Exception.Message)" -ForegroundColor Red
86 + Write-Host "FAILED" -ForegroundColor Red
87 + if ($tcpClient) { $tcpClient.Close() }
88 + }
89 +
90 + # Port 27017: Check for MongoDB (HTTP 200/404 with body)
91 + try {
92 + $uri = "http://$($HostName):27017/"
93 + Write-Host "Attempting to connect to $uri" -ForegroundColor Cyan
94 + $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -TimeoutSec 5 -ErrorAction SilentlyContinue
95 + if ($response -and ($response.StatusCode -eq 200 -or $response.StatusCode -eq 404)) {
96 + if ($response.Content.Length -gt 0) {
97 + # Check for MongoDB-specific content
98 + if ($response.Content -match "MongoDB" -or $response.Content -match "db" -or $response.Content -match "version") {
99 + Write-Host "Port 27017: Success - MongoDB detected (Status $($response.StatusCode) with body)" -ForegroundColor Green
100 + Write-Host "OK" -ForegroundColor Green
101 + } else {
102 + Write-Host "Port 27017: Success - Status $($response.StatusCode) with body, but not MongoDB" -ForegroundColor Yellow
103 + Write-Host "OK" -ForegroundColor Green
104 + }
105 + } else {
106 + Write-Host "Port 27017: Failure - Status $($response.StatusCode) but empty body" -ForegroundColor Red
107 + Write-Host "FAILED" -ForegroundColor Red
108 + }
109 + } else {
110 + Write-Host "Port 27017: Failure - Catch - $(if ($response) { "Unexpected status $($response.StatusCode)" } else { "No response received: $($_.Exception.Message)" })" -ForegroundColor Red
111 + Write-Host "FAILED" -ForegroundColor Red
112 + }
113 + } catch {
114 + Write-Host "Port 27017: Failure - Catch - $($_.Exception.Message)" -ForegroundColor Red
115 + Write-Host "FAILED" -ForegroundColor Red
116 + }
117 +
118 + # Port 8020: Web service (HTTP 200/404, body optional)
119 + try {
120 + $uri = "http://$($HostName):8020/"
121 + Write-Host "Attempting to connect to $uri" -ForegroundColor Cyan
122 + $request = [System.Net.HttpWebRequest]::Create($uri)
123 + $request.Method = "GET"
124 + $request.Timeout = 5000 # 5-second timeout
125 + try {
126 + $response = $request.GetResponse()
127 + $statusCode = [int]$response.StatusCode
128 + $response.Close()
129 + Write-Host "Port 8020: Debug - Response received, StatusCode: $statusCode" -ForegroundColor Yellow
130 + if ($statusCode -eq 200 -or $statusCode -eq 404) {
131 + Write-Host "Port 8020: Success - Web service detected (Status $statusCode)" -ForegroundColor Green
132 + Write-Host "OK" -ForegroundColor Green
133 + } else {
134 + Write-Host "Port 8020: Failure - Unexpected status $statusCode" -ForegroundColor Red
135 + Write-Host "FAILED" -ForegroundColor Red
136 + }
137 + } catch {
138 + $statusCode = $null
139 + if ($_.Exception.InnerException -is [System.Net.WebException]) {
140 + $webResponse = $_.Exception.InnerException.Response
141 + if ($webResponse) {
142 + $statusCode = [int]$webResponse.StatusCode
143 + $webResponse.Close()
144 + Write-Host "Port 8020: Debug - Response received, StatusCode: $statusCode" -ForegroundColor Yellow
145 + if ($statusCode -eq 200 -or $statusCode -eq 404) {
146 + Write-Host "Port 8020: Success - Web service detected (Status $statusCode)" -ForegroundColor Green
147 + Write-Host "OK" -ForegroundColor Green
148 + } else {
149 + Write-Host "Port 8020: Failure - Unexpected status $statusCode" -ForegroundColor Red
150 + Write-Host "FAILED" -ForegroundColor Red
151 + }
152 + } else {
153 + Write-Host "Port 8020: Failure - Inner Catch - No response received: $($_.Exception.Message)" -ForegroundColor Red
154 + Write-Host "FAILED" -ForegroundColor Red
155 + }
156 + } else {
157 + Write-Host "Port 8020: Failure - Inner Catch - $($_.Exception.Message)" -ForegroundColor Red
158 + Write-Host "FAILED" -ForegroundColor Red
159 + }
160 + }
161 + } catch {
162 + Write-Host "Port 8020: Failure - Outer Catch - $($_.Exception.Message)" -ForegroundColor Red
163 + Write-Host "FAILED" -ForegroundColor Red
164 + }
Siguiente Anterior