cmdudpnetcat

How to send a hexadecimal byte array using the command line?


I want to send a byte array to a device in my network with IP address 192.168.0.80 via UDP to port 8888.

How can I do that from a command prompt window or a batch script?

The hex data to send (without the commas): 02, 01, 65, C9, 00, 02, 01, 06


Solution

  • On Linux can be used echo to pipe a string to netcat like this:

    echo -ne "\x2\x1\x65\xc9\x0\x2\x1\x6" | netcat -u -q 0 192.168.0.80 8888
    

    Make sure to remove trailing zeros by first replacing ,0 with \x and then replace all remaining , with \x using search and replace in Notepad++.


    On Windows must be used PowerShell.
    Create a file and save it as SendUdp.ps1.

    param(
        [string]$remoteIP,
        [int]$remotePort,
        [byte[]]$byteArray
    )
    
    $udpClient = New-Object System.Net.Sockets.UdpClient
    $remoteEndPoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse($remoteIP), $remotePort)
    $udpClient.Send($byteArray, $byteArray.Length, $remoteEndPoint)
    $udpClient.Close()
    

    Then can be executed this PowerShell script via the normal command line:

    .\SendUdp.ps1 -remoteIP "192.168.1.100" -remotePort 5000 -byteArray @(0x02,0x01,0x65,0xc9,0x00,0x02,0x01,0x06)