如果我想使用TCP将位于与NetCat相同目录下的二进制文件"binary.bin"传输到IP地址为127.0.0.1,端口为1200的计算机上,我该如何使用Windows版的NetCat进行指定?
nc 127.0.0.1 1200 < binary.bin
nc 127.0.0.1 1200 < binary.bin > response.bin
我想与您分享一个完整的解决方案,您可以使用它通过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"
}
希望这能帮助你节省一些时间;)
{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat path/to/your/file/binary.bin ; } | nc -l 1200