How to Continuously Monitor Port Availability in Windows (Alternative to Ping)


11 views

While ping is excellent for basic network connectivity checks (ICMP), it doesn't help when you need to verify TCP port availability. This becomes crucial when:

  • Monitoring web servers (port 80/443)
  • Checking database connectivity (3306, 5432, etc.)
  • Verifying application-specific ports
  • Testing firewall rules

Windows provides several built-in utilities for port testing:

Test-NetConnection -ComputerName 192.168.1.1 -Port 8080

For continuous monitoring in PowerShell:

while ($true) {
    $result = Test-NetConnection -ComputerName 192.168.1.1 -Port 8080 -InformationLevel Quiet
    "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Port status: $result" | Out-File -Append port_monitor.log
    Start-Sleep -Seconds 5
}

While not ideal for continuous monitoring, telnet provides quick manual checks:

telnet 192.168.1.1 3389

For more robust monitoring, consider:

# Using Nmap (requires installation)
nmap -p 80 -Pn 192.168.1.1

# Continuous monitoring with Nmap
while true; do
    nmap -p 80 -Pn 192.168.1.1 | find "80/tcp"
    timeout /t 5 > nul
done

For enterprise-grade monitoring:

$port = 8080
$server = "192.168.1.1"
$timeout = 1000 # milliseconds
$logPath = "C:\logs\port_monitor.csv"

while ($true) {
    $tcpClient = New-Object System.Net.Sockets.TcpClient
    $connectTask = $tcpClient.ConnectAsync($server, $port)
    
    if ($connectTask.Wait($timeout)) {
        $status = if ($tcpClient.Connected) { "Open" } else { "Closed" }
    } else {
        $status = "Timeout"
    }
    
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    "$timestamp,$server,$port,$status" | Out-File -Append -FilePath $logPath
    
    $tcpClient.Dispose()
    Start-Sleep -Seconds 10
}

For comprehensive monitoring:

  1. Open perfmon.exe
  2. Add new Data Collector Set
  3. Select "TCPv4" or "TCPv6" counters
  4. Monitor "Connections Established" and "Connection Failures"

After collecting data, you can analyze it with:

# PowerShell to visualize recent data
Import-Csv $logPath | 
Where-Object { $_.Timestamp -gt (Get-Date).AddHours(-24) } |
Group-Object Status |
Select-Object Count,Name |
Sort-Object Count -Descending

While the ping command is excellent for basic ICMP-based host availability checks, it falls short when we need to verify TCP/UDP port availability. Many production environments require continuous monitoring of specific service ports (like 80, 443, 3389, etc.) rather than just host reachability.

Windows provides several built-in alternatives:

1. Test-NetConnection (PowerShell):
Test-NetConnection 192.168.1.1 -Port 80 -InformationLevel Detailed

2. PortQry (Microsoft's dedicated tool):
portqry -n 192.168.1.1 -e 80 -nr

For persistent monitoring like your ping -t example, consider these approaches:

PowerShell Loop Method

while($true) {
    $result = Test-NetConnection 192.168.1.1 -Port 3389
    if($result.TcpTestSucceeded) {
        "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Port OPEN" | Out-File port_monitor.log -Append
    }
    else {
        "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Port CLOSED" | Out-File port_monitor.log -Append
    }
    Start-Sleep -Seconds 5
}

Nmap for Advanced Scanning

The popular Nmap tool offers richer functionality:

nmap -p 80 --open -T4 192.168.1.1 -oG - | find "80/open"

When dealing with firewalls that block ICMP, TCP-based checks are essential. The PowerShell method above works through firewalls when proper exceptions exist.

  • TCPing: Lightweight console tool mimicking ping behavior for ports
  • PortMon: Graphical port monitoring utility
  • PsPing: Part of Sysinternals suite with advanced capabilities

For production environments, consider dedicated monitoring tools like:

  • PRTG Network Monitor
  • SolarWinds Port Monitor
  • Nagios with check_tcp plugin