开发者

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

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?


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


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.


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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜