Linux Bash 中的简单套接字服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4739196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:10:10  来源:igfitidea点击:

Simple Socket Server in Bash?

linuxbashsocketstcp

提问by Naftuli Kay

Is there a way to quickly bind to a TCP port/ip address and simply print out all information to STDOUT? I have a simple debugging solution which writes things to 127.0.0.1:4444 and I'd like to be able to simply bind up a port from bash and print everything that comes across. Is there an easy way to do this?

有没有办法快速绑定到 TCP 端口/IP 地址并简单地将所有信息打印到 STDOUT?我有一个简单的调试解决方案,可以将内容写入 127.0.0.1:4444,我希望能够简单地从 bash 绑定一个端口并打印遇到的所有内容。是否有捷径可寻?

采纳答案by Nikolai Fetissov

$ nc -k -l 4444 > filename.out

see nc(1)

nc(1)

回答by Diego Torres Milano

Just because you asked how to do it in bash, though netcatanswer is very valid:

仅仅因为您在 中询问了如何做到这一点bash,尽管netcat答案非常有效:

  $ exec 3<>/dev/tcp/127.0.0.1/4444
  $ cat <&3

回答by luk

That is working as you expecting:

这正如您所期望的那样工作:

 nc -k -l 4444 |bash

and then you

然后你

echo "ls" >/dev/tcp/127.0.0.1/4444

then you see the listing performed by bash.

然后您会看到 bash 执行的列表。

[A Brief Security Warning]
Of course if you leave a thing like this running on your computer, you have a wide open gateway for all kinds of attacks because commands can be sent from any user account on any host in your network. This implements no security (authentication, identification) whatsoever and sends all transmitted commands unencrypted over the network, so it can very easily be abused.

[简短的安全警告]
当然,如果你让这样的东西在你的计算机上运行,​​你就有了一个可以应对各种攻击的开放网关,因为可以从网络中任何主机上的任何用户帐户发送命令。这不会实现任何安全性(身份验证、识别),并且会通过网络发送所有传输的未加密命令,因此很容易被滥用。

回答by Kilokahn

Adding an answer using ncatthat @Freedom_Ben alluded to:

添加使用ncat@Freedom_Ben 提到的答案:

ncat -k -l 127.0.0.1 4444

and explanation of options from man ncat:

和 man ncat 选项的解释:

-k, --keep-open            Accept multiple connections in listen mode
-l, --listen               Bind and listen for incoming connections