开发者

How to make dynamic response with bash

I'm trying to listen socket into bash to produce dynamic response. I tried do like this nc -l 8088 -c``echo 'Request got, let's produce it' but -c option is deprecated and unsupported now. Then I tried to list port using /dev/tcp: exec 3<>/dev/tcp/127.0.0.1/8088; cat <&3 But i've got an error:

开发者_C百科connect: Connection denied* 
line 1: /dev/tcp/127.0.0.1/8088: Connection denied*
line 2: 3: Wrong file descriptor*
  • Translated from russian word for word


That syntax, according to the Bash manpage, is for opening a connection to an existing socket. I don't know of an option to create a socket using only Bash. And your nc command uses port 8000 not 8088.

[update] OK then, but you're also missing the -p switch to set the port. Didn't catch that before.

jcomeau@intrepid:~$ nc -l -p8088 -c 'echo bleah' &
[1] 4752
jcomeau@intrepid:~$ exec 3<>/dev/tcp/127.0.0.1/8088; cat <&3
bleah

For an example of using pipes, see the bottom of this page, in the explanation of a proxy: http://www.stearns.org/doc/nc-intro.v0.9.html, hopefully you can adapt it for your needs.

Why not use inetd? The main problem with using netcat with Bash is that there's no straightforward way that I can think of to send back a reply, because you don't know what port they used to connect with. Whereas inetd handles the tcp/ip connection for you, letting your shell script just deal with stdin and stdout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜