Checking a remote udp port

Welcome Forums General PowerShell Q&A Checking a remote udp port

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #103601

    Participant
    Points: 0
    Rank: Member

    Good day.
    I am checking the availability and responsiveness of a remote udp port:

    $Udpclient=new-Object system.Net.Sockets.Udpclient
    $Udpclient.client.ReceiveTimeout=20000
    $Udpclient.Connect('',) 
    $AsciiEncoder = new-object system.text.asciiencoding 
    $byte = $AsciiEncoder.GetBytes("Anybody there?") 
    $Udpclient.Send($byte,$byte.length) 
    $IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 
    $ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint) 
    $AsciiEncoder.GetString($ReceiveBytes)
    $Udpclient.close()
    

    When i debug it i can see that all is correct until this line is executed:
    $ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint)
    It times out after 20 sec with the following exception:
    Exception calling “Receive” with “1” argument(s): “A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond”

    When i test the availability of the same port with Microsoft's port query utility i get success in 5 sec:
    portqry.exe -n servername -e portnumber -p UDP

    i also tried the Test-Port PS script and it failed.
    i am puzzled.
    Can you help?

    Regards

  • #103603
    js

    Participant
    Points: 8
    Rank: Member

    There's an ancient saying: when in doubt, use Process Monitor.

    I get a syntax error with this line:

    $Udpclient.Connect('',)
    
    + $Udpclient.Connect('',)
    +                       ~
    Missing expression after ','.
    

    You're trying to connect to any address port 0?

    $IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 
    • #103645

      Participant
      Points: 0
      Rank: Member
      $Udpclient.Connect('',)

      was a typo, sorry. it should have read

      $Udpclient.Connect(servername, portnumber)

      substitute servername with a UNC server name and portnumber with udp port on that server.

      as to this line

      $IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)

      it is used in practically all examples as a broad range endpoint which allows communication to come from all ip addresses and all ports.

      All the best.

  • #103613

    Participant
    Points: 28
    PublishedHelping Hand
    Rank: Member

    See if this guidance helps, that is if you have not already seen it.

    'https://learn-powershell.net/2011/02/21/querying-udp-ports-with-powershell'
    'https://learn-powershell.net/2011/10/23/querying-udp-ports-with-powershell-part-2'

  • #103648

    Participant
    Points: 0
    Rank: Member

    Yes, Postanote, i read both articles. In fact the code that i put together is basically a scalleton code from one of the examples.

    All the best.

  • #103652
    js

    Participant
    Points: 8
    Rank: Member

    The test-port here function worked for me (https://gallery.technet.microsoft.com/scriptcenter/97119ed6-6fb2-446d-98d8-32d823867131), at least with one of Comcast's dns servers. I guess it depends on if the udp server responds to garbage. It's not like you can test making a syn connection with tcp. The code at the bottom of https://learn-powershell.net/2011/10/23/querying-udp-ports-with-powershell-part-2 didn't work for me. Strange, sending "TEST" to the Comcast dns server gets no response, but "Anybody there?" does. test-port sends the date.

    PS /Users/js/Downloads> Test-Port -comp cdns01.comcast.net -port 53 -udp -verbose
    
    VERBOSE: Making UDP connection to remote server
    VERBOSE: Sending message to remote host
    VERBOSE: Creating remote endpoint
    VERBOSE: Waiting for message return
    VERBOSE: Connection Successful
    
    
    Server   : cdns01.comcast.net
    Port     : 53
    TypePort : UDP
    Open     : True
    Notes    : 07??

    Also osx has nc (netcat):

    PS /Users/js> nc -zuv cdns01.comcast.net 53                       
                                                    
    found 0 associations
    found 1 connections:
         1:	flags=82
    	outif (null)
    	src 192.168.1.108 port 50158
    	dst 75.75.75.75 port 53
    	rank info not available
    
    Connection to cdns01.comcast.net port 53 [udp/domain] succeeded!

    Actually your code works for me. The 14 is from updclient.send() and asciiencoder.getstring() returns the weird characters.

    $Udpclient = new-Object system.Net.Sockets.Udpclient
    $Udpclient.client.ReceiveTimeout = 1000
    $Udpclient.Connect('cdns01.comcast.net',53)
    $AsciiEncoder = new-object system.text.asciiencoding
    $byte = $AsciiEncoder.GetBytes("Anybody there?")
    $Udpclient.Send($byte,$byte.length)
    $IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
    $ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint)
    $AsciiEncoder.GetString($ReceiveBytes)
    $Udpclient.close()
    
    14
    An??
    • #103922

      Participant
      Points: 0
      Rank: Member

      Good day.
      I've tinkered a bit more.
      $Udpclient.Connect opens a local listening udp port and sets accordingly the $Udpclient.client.localendpoint
      So after $Udpclient.Connect one can see among udp ports the one that $Udpclient.client.localendpoint describes.
      I went to the remote server and tried to portqry.exe that port on my local machine and failed.
      With that i declared the problem a cold case.
      As to asciiencoder.getstring() returning weird characters it seems to me that you hit upon a service that takes whatever string you send to it and leaves only first two and the last character.

      All the best.

The topic ‘Checking a remote udp port’ is closed to new replies.