How to use NetCat for Windows to send a binary file to a TCP connection?
Asked Answered
U

3

10

If I want to transfer a binary file "binary.bin" (located in the same directory as NetCat) to IP address 127.0.0.1 port 1200 using TCP, how do I specify this using NetCat for windows?

Unfolded answered 11/1, 2010 at 9:18 Comment(0)
U
9

I found the solution. Its

nc 127.0.0.1 1200 < binary.bin

In addition, if the response needs to be saved then

nc 127.0.0.1 1200 < binary.bin > response.bin
Unfolded answered 12/1, 2010 at 3:24 Comment(0)
L
0

If you want to send it from a linux distribution to anybody, including windows PCs you can do something like this:

{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat path/to/your/file/binary.bin ; } | nc -l 1200

The receiving end then just needs to browse to your IP Address in his web browser and gets a prompt to download. Needless to say port 1200 needs to be forwarded if you're behind a router.

Lag answered 27/12, 2018 at 23:46 Comment(0)
R
0

I just want to share with you the complete solution that you can use to expose/make available a file via a network between remote computers with Netcat.

I use this perfect and simple solution to share a file between Docker Containers in a portable way without defining Docker Volume or installing ssh in the Docker container.

A simple bash function that exposes the file on the source machine:

# ------------------------------------------------------------------------------
# make the file available for another machine via the network
#
# this runs in the background to avoid blocking the main script execution
# ------------------------------------------------------------------------------
function exposeFile() {
    local file port
    file="$1"
    port=1384

    echo "exposing the file for another machine with..."
    echo "   file: $file"
    echo "   port: $port"

    while :
    do
        { echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat "$file" ; } | nc  -l "$port"
    done
}

The endless loop is necessary if you want to download the file more than one time because after the download Netcat exists.

Call the bash method from your main script to expose the file when you need it:

#!/bin/bash
...
exposeFile "path/to/the/file.zip" &

Then you can use the a simple wget to download the file on the source machine:

function fileDownload {
    echo "downloading the file..."

    local fileHome file
    fileHome="/download/directory/"
    file="myfile.zip"

    local remoteHost remotePort
    remoteHost="remote-host-or-ip"
    remotePort=1384

    mkdir -p "$fileHome"
    wget -O "$fileHome/$file" "$remoteHost":"$remotePort"
}

I hope that this will help you to save some time ;)

Riehl answered 14/4, 2022 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.