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

有没有一种方法可以快速绑定到 TCP 端口/ip 地址并简单地将所有信息打印到 STDOUT?我有一个简单的调试解决方案,可以将数据写到127.0.0.1:4444,我希望能够简单地从 bash 绑定一个端口,并打印所有遇到的内容。有什么简单的方法吗?

111047 次浏览
$ nc -k -l 4444 > filename.out

see nc(1)

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

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

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.

[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.

Adding an answer using ncat that @Freedom_Ben alluded to:

ncat -k -l 127.0.0.1 4444

and explanation of options from man ncat:

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