如何使用Windows的NetCat将二进制文件发送到TCP连接?

9
如果我想使用TCP将位于与NetCat相同目录下的二进制文件"binary.bin"传输到IP地址为127.0.0.1,端口为1200的计算机上,我该如何使用Windows版的NetCat进行指定?
3个回答

9
我找到了解决方案。它的
nc 127.0.0.1 1200 < binary.bin

此外,如果需要保存响应,则需执行以下操作:
nc 127.0.0.1 1200 < binary.bin > response.bin

0

我想与您分享一个完整的解决方案,您可以使用它通过Netcat在远程计算机之间公开/提供文件。

我使用这个完美而简单的解决方案以便以便携方式在Docker容器之间共享文件,而无需定义Docker Volume或在Docker容器中安装ssh

一个简单的bash函数,在源机器上公开文件:

# ------------------------------------------------------------------------------
# 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
}

如果您想要多次下载文件,无限循环是必要的,因为在下载完成后Netcat会退出。

从您的主脚本中调用bash方法,在需要时公开该文件:

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

然后您可以使用简单的 wget 在源机器上下载文件:

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"
}

希望这能帮助你节省一些时间;)


0
如果您想将其从Linux发给任何人,包括Windows PC,可以执行以下操作:
{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat path/to/your/file/binary.bin ; } | nc -l 1200

接收方只需在其Web浏览器中浏览到您的IP地址,然后会收到一个提示以进行下载。 不用说,如果您在路由器后面,则需要转发端口1200。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接