Testing for Blocked UDP Ports
Firewalls: A love-hate relationship. When the firewall administrator has the appropriate exceptions in place, it’s mostly protecting the one you love most. But when they don’t, it feels like betrayal.
Alright, so my metaphor is off, but it is frustrating to find that, after hours of troubleshooting, that pesky application performance or availability issue was caused by a missing exception in a firewall somewhere between Host A and Host B. So more often than not, testing for a blocked port is on my troubleshooting short list these days.
So how does one test for a blocked port between two hosts? For TCP/IP ports, there are many options available, including nmap. But assuming the listener is running, I usually use a telnet client to attempt to connect to the port. An elegant test, it is not. But in most cases, it does the job just fine (as long as you have a telnet client installed).
But what about UDP ports? UDP is a different beast because it is a connectionless protocol. In other words, you can send something to the client, but don’t expect to receive a response indication of success in return. It’s true that you can sometimes determine whether a UDP port is open through alternate means, but isn’t necessarily fool-proof. Instead, the method I use is to set up a listener on the UDP port in question on the receiving host and then UDP packets to that host and port from the sending host (that is, the host that typically does the sending the UDP packets). The best part is, I can do it all using two very simple PowerShell scripts. Below are the scripts, followed by instructions.
Receive-UDPMessage.ps1
#Waits for a UDP message on a particular port.
Param(
[parameter(Mandatory=$True,Position=0, HelpMessage='The host UDP port to send the message to')]
[Int]$Port,
[parameter(Mandatory=$False,Position=1, HelpMessage='If set, the function will continue listening for messages instead of exiting after the first one it receives. ')]
[switch]$Loop=$False
)
function Receive-UDPMessage{
[CmdletBinding(
DefaultParameterSetName='Relevance',
SupportsShouldProcess=$False
)]
Param(
[parameter(Mandatory=$True,Position=0, HelpMessage='The host UDP port to send the message to')]
[Int]$Port,
[parameter(Mandatory=$False,Position=1, HelpMessage='If set, the function will continue listening for messages instead of exiting after the first one it receives. ')]
[switch]$Loop=$False
)
try {
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Any,$port)
$udpclient=new-Object System.Net.Sockets.UdpClient $port
do {
Write-Host "Waiting for message on UDP port $Port..."
Write-Host ""
$content=$udpclient.Receive([ref]$endpoint)
Write-Host "Received: $content"
write-host "Received message: $([Text.Encoding]::ASCII.GetString($content))"
Write-Host "Received from: $($endpoint.address.toString()):$($endpoint.Port)"
} while($Loop)
}catch [system.exception] {
throw $error[0]
} finally {
$udpclient.Close()
}
}
Receive-UDPMessage -Port $Port $Loop
Send-UDPMessage.ps1
#Sends a message to a host on a particular port.
Param(
[parameter(Mandatory=$True,Position=0, HelpMessage='The host to send the message to')]
[String]$Hostname,
[parameter(Mandatory=$True,Position=1, HelpMessage='The message to send')]
[String]$Message,
[parameter(Mandatory=$True,Position=2, HelpMessage='The host UDP port to send the message to')]
[Int]$Port
)
function Send-UDPMessage{
[CmdletBinding(
DefaultParameterSetName='Relevance',
SupportsShouldProcess=$False
)]
Param(
[parameter(Mandatory=$True,Position=0, HelpMessage='The host to send the message to')]
[String]$Hostname,
[parameter(Mandatory=$True,Position=1, HelpMessage='The message to send')]
[String]$Message,
[parameter(Mandatory=$True,Position=2, HelpMessage='The host UDP port to send the message to')]
[Int]$Port
)
Write-Host "Message to send: $Message"
$udpclient=new-Object System.Net.Sockets.UdpClient
$b=[Text.Encoding]::ASCII.GetBytes($Message)
$bytesSent=$udpclient.Send($b,$b.length,$Hostname, $Port)
write-host "Sent: $b"
$udpclient.Close()
}
Send-UDPMessage -Hostname $Hostname -Message $Message -Port $Port
Using these scripts is simple:
- First, you run Receive-UDPMessage.ps1 on the receiving host like so:

Note: If the script can’t bind to the port because it is being used by another application, it will throw an error. If that happens, you will need to temporarily stop that application. - Next, you run Send-UDPMessage.ps1 on the sending host like so:

- If UDP port isn’t blocked, the receiving host should get the message the script will end. If it is blocked, you’ll get nothing.

For a sanity check, you can also try running both scripts on the same host to confirm it works (use localhost for the Hostname parameter).
Before I run off and claim sole credit for these scripts, I need to say that the original idea for them came from this page. I merely refined things a bit. Best of luck with your UDP troubleshooting!
![[del.icio.us]](http://iboyd.net/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://iboyd.net/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://iboyd.net/wp-content/plugins/bookmarkify/facebook.png)
![[StumbleUpon]](http://iboyd.net/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://iboyd.net/wp-content/plugins/bookmarkify/twitter.png)


