echo command | netcat host port
Run Code Online (Sandbox Code Playgroud)
这导致命令被发送到远程主机并且一些数据被读回。但几秒钟后,连接关闭。-w 参数没有改变任何东西。我在 SuSE 10.1 上使用 netcat v1.10。
Cha*_*pta 40
这适用nc
于 OS X 上的命令(假设您要发送的命令在文件中):
cat file - | nc host port
Run Code Online (Sandbox Code Playgroud)
(本质上,cat
将文件的内容转储到标准输出上,然后在标准输入上等待您)。
通过扩展,如果你想从 shell 本身发送命令,你可以这样做:
cat <(echo command) - | nc host port
Run Code Online (Sandbox Code Playgroud)
Suz*_*Soy 16
随着nc
Ubuntu上:
nc -q -1 host port
Run Code Online (Sandbox Code Playgroud)
-q seconds
after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
Run Code Online (Sandbox Code Playgroud)
请注意,发行版nc
之间的可用选项差异很大,因此这可能不适用于您的 (OpenSUSE)。
Chr*_*ris 13
我找到了:
echo command | netcat host port -
Run Code Online (Sandbox Code Playgroud)
我的同事知道。我根本没有在文档中看到这一点。
可能是套接字另一端的连接已关闭?
默认情况下,nc
如果您没有明确告诉他保持监听(使用选项-k
),则在完成后关闭连接:
-k Forces nc to stay listening for another connection after its current
connection is completed. It is an error to use this option without the
-l option.
Run Code Online (Sandbox Code Playgroud)
看man nc.1
。
我成功地在两台机器之间传输数据,如下所示:
发件人:
while (true); do
echo -n "$RANDOM " | nc <host> <port>
done
Run Code Online (Sandbox Code Playgroud)接收者:
nc -kl <port>
Run Code Online (Sandbox Code Playgroud)我认为您不会使用 netcat 或 socat 来管理此问题。我刚刚对两者进行了广泛的修改,socat 看起来最有前途。
我设法将 socat 设置为连接到远程 TCP 端口并侦听本地 unix 域套接字(理论上,这样链接可以一直保持),但是一旦本地进程与 unix 套接字(另一个 socat将 unix 套接字链接到 stdin/out)它关闭了 TCP socat 会话。
这里的问题是,通过 netcat / socat 的每个连接都会与服务器建立一个新的 TCP 流连接,并且当本地端断开连接时,它会关闭该 TCP 流会话。
我认为您可能必须为此编写一些自定义代理软件,该软件打开到远程端的 TCP 连接,然后在套接字/管道/fifo 或其他任何设备上本地侦听,然后将数据发送到现有的 TCP 管道并返回结果。