Repository navigation
Expand file tree
/
Copy pathNetwork-testAport.ps1
More file actions
280 lines (233 loc) · 9.76 KB
/
Copy pathNetwork-testAport.ps1
File metadata and controls
280 lines (233 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<#
.SYNOPSIS
Tests network connectivity to a specific IP address and port.
.DESCRIPTION
This script uses PowerShell's Test-NetConnection cmdlet to verify if a specific
port is open and accessible on a target IP address. This is useful for:
- Testing SSH connectivity (port 22)
- Verifying web server accessibility (ports 80, 443)
- Checking database connections (ports 3306, 1433, 5432, etc.)
- Troubleshooting network firewall issues
- Validating service availability before connecting
.PARAMETER address
The IP address or hostname to test connectivity to.
Default: 192.168.1.212
.PARAMETER port
The port number to test connectivity on.
Default: 22 (SSH)
.EXAMPLES
# Test SSH connectivity to default address
.\Network-testAport.ps1
# Test web server on port 80
$address = "192.168.1.100"; $port = 80; Test-NetConnection $address -Port $port
# Test HTTPS on port 443
$address = "google.com"; $port = 443; Test-NetConnection $address -Port $port
# Test MySQL database on port 3306
$address = "db.company.com"; $port = 3306; Test-NetConnection $address -Port $port
.OUTPUTS
Returns a NetTcpConnection object with properties:
- ComputerName: Target computer/IP
- RemoteAddress: Resolved IP address
- RemotePort: Target port number
- InterfaceAlias: Local network interface used
- SourceAddress: Local IP address used
- TcpTestSucceeded: Boolean indicating if connection was successful
- PingSucceeded: Boolean indicating if ping was successful
.NOTES
- Requires PowerShell 4.0 or later
- Test-NetConnection replaces the older telnet method for port testing
- No special privileges required for most ports
- Firewall rules may block certain port tests
- Some services may not respond to connection tests even if running
.AUTHOR
Mike Brown
.VERSION
1.0
.DATE
2025-10-10
#>
#Powershell command to see if port is working
# Enhanced Port Testing Function
function Test-NetworkPort {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$Address,
[Parameter(Mandatory=$false)]
[int]$Port,
[Parameter(Mandatory=$false)]
[int]$Timeout = 5000,
[Parameter(Mandatory=$false)]
[switch]$Quiet
)
# Common ports for quick selection
$commonPorts = @{
"SSH" = 22
"Telnet" = 23
"SMTP" = 25
"DNS" = 53
"HTTP" = 80
"POP3" = 110
"HTTPS" = 443
"SMB" = 445
"SMTP Secure" = 587
"IMAP" = 993
"POP3 Secure" = 995
"SQL Server" = 1433
"MySQL" = 3306
"RDP" = 3389
"PostgreSQL" = 5432
"RabbitMQ" = 5672
"RabbitMQ Management" = 15672
"Redis" = 6379
"Kafka" = 9092
"Kafka JMX" = 9999
"Elasticsearch" = 9200
"Kibana" = 5601
"MongoDB" = 27017
"Zookeeper" = 2181
}
# Interactive mode if no parameters provided
if (-not $Address) {
Write-Host "=== Network Port Tester ===" -ForegroundColor Cyan
Write-Host "Test connectivity to any IP address and port" -ForegroundColor Gray
Write-Host ""
$Address = Read-Host "Enter IP address or hostname"
if ([string]::IsNullOrWhiteSpace($Address)) {
$Address = "192.168.1.212" # Default
Write-Host "Using default address: $Address" -ForegroundColor Yellow
}
}
if (-not $Port) {
Write-Host "`nCommon Ports:" -ForegroundColor Green
# Create formatted port display in multiple columns
$portList = $commonPorts.GetEnumerator() | Sort-Object Value | ForEach-Object {
[PSCustomObject]@{
Port = $_.Value
Service = $_.Key
Display = "$($_.Value) - $($_.Key)"
}
}
# Display in 3 columns for better space usage
$columnCount = 3
$rowCount = [Math]::Ceiling($portList.Count / $columnCount)
for ($row = 0; $row -lt $rowCount; $row++) {
$line = ""
for ($col = 0; $col -lt $columnCount; $col++) {
$index = $row + ($col * $rowCount)
if ($index -lt $portList.Count) {
$item = $portList[$index]
$line += " {0,-25}" -f $item.Display
}
}
Write-Host $line -ForegroundColor Gray
}
Write-Host ""
$portInput = Read-Host "Enter port number (or press Enter for SSH/22)"
if ([string]::IsNullOrWhiteSpace($portInput)) {
$Port = 22 # Default to SSH
Write-Host "Using default port: $Port (SSH)" -ForegroundColor Yellow
} else {
$Port = [int]$portInput
}
}
# Validate inputs
if ($Port -lt 1 -or $Port -gt 65535) {
Write-Host "Error: Port must be between 1 and 65535" -ForegroundColor Red
return
}
# Identify service type
$serviceType = $commonPorts.GetEnumerator() | Where-Object { $_.Value -eq $Port } | Select-Object -First 1
$serviceName = if ($serviceType) { $serviceType.Name } else { "Unknown Service" }
if (-not $Quiet) {
Write-Host "`n=== Testing Connection ===" -ForegroundColor Cyan
Write-Host "Target: $Address" -ForegroundColor White
Write-Host "Port: $Port ($serviceName)" -ForegroundColor White
Write-Host "Timeout: $($Timeout/1000) seconds" -ForegroundColor Gray
Write-Host "Testing..." -ForegroundColor Yellow
}
try {
# Perform the connection test with minimal output
$result = Test-NetConnection -ComputerName $Address -Port $Port -InformationLevel Quiet -WarningAction SilentlyContinue
if (-not $Quiet) {
Write-Host "`n=== Results ===" -ForegroundColor Cyan
if ($result.TcpTestSucceeded) {
Write-Host "[SUCCESS] Port $Port is OPEN on $Address" -ForegroundColor Green
Write-Host "[SUCCESS] Service appears to be running" -ForegroundColor Green
} else {
Write-Host "[FAILED] Port $Port is CLOSED or FILTERED on $Address" -ForegroundColor Red
Write-Host "[FAILED] Service may be down, blocked by firewall, or not installed" -ForegroundColor Red
}
# Only show essential connection info
Write-Host "`nConnection Summary:" -ForegroundColor Yellow
Write-Host " Target: $Address" -ForegroundColor Gray
Write-Host " Port: $Port ($serviceName)" -ForegroundColor Gray
Write-Host " Result: $(if ($result.TcpTestSucceeded) { 'CONNECTED' } else { 'FAILED' })" -ForegroundColor $(if ($result.TcpTestSucceeded) { 'Green' } else { 'Red' })
# Provide helpful suggestions
Write-Host "`nTroubleshooting Tips:" -ForegroundColor Cyan
if (-not $result.TcpTestSucceeded) {
Write-Host " * Check if the service is running on the target machine" -ForegroundColor White
Write-Host " * Verify firewall rules on both source and destination" -ForegroundColor White
Write-Host " * Ensure the port number is correct for the service" -ForegroundColor White
if ($Port -in @(22, 23, 3389)) {
Write-Host " * For remote access services, check if they are enabled" -ForegroundColor White
}
} else {
Write-Host " * Connection successful! You can now connect to this service" -ForegroundColor White
if ($Port -eq 22) {
Write-Host " * Try: ssh username@$Address" -ForegroundColor Cyan
} elseif ($Port -eq 80) {
Write-Host " * Try: http://$Address" -ForegroundColor Cyan
} elseif ($Port -eq 443) {
Write-Host " * Try: https://$Address" -ForegroundColor Cyan
} elseif ($Port -eq 3389) {
Write-Host " * Try: mstsc /v:$Address" -ForegroundColor Cyan
}
}
Write-Host "`nNote: For detailed network diagnostics, use the Network-DetailedDiagnostics.ps1 script" -ForegroundColor DarkGray
}
# Return only essential information for scripting
return [PSCustomObject]@{
ComputerName = $Address
RemotePort = $Port
TcpTestSucceeded = $result.TcpTestSucceeded
ServiceName = $serviceName
}
} catch {
if (-not $Quiet) {
Write-Host "`nError occurred during testing:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
}
return $null
}
}
# Quick test function for scripting
function Test-PortConnection {
param(
[Parameter(Mandatory=$true)]
[string]$Address,
[Parameter(Mandatory=$true)]
[int]$Port
)
$result = Test-NetworkPort -Address $Address -Port $Port -Quiet
if ($result) {
return $result.TcpTestSucceeded
} else {
return $false
}
}
# Default execution - maintain backward compatibility
if ($MyInvocation.MyCommand.Path -eq $PSCommandPath) {
# Script is being run directly, not dot-sourced
# Interactive loop for multiple port tests
do {
Test-NetworkPort
Write-Host "`n" -NoNewline
$testAnother = Read-Host "Would you like to test another port? (Y/N)"
if ($testAnother -notmatch '^[Yy]') {
Write-Host "`nThank you for using Network Port Tester!" -ForegroundColor Cyan
break
}
Write-Host "`n" + ("="*50) -ForegroundColor DarkGray
} while ($true)
}