开发者

Emulating netcat -e

How can I emulate netcat -e with a version of netcat that does not have the -e option ?

I need to trigger a command remotely. I can do it with netcat - without -e:

#!/bin/bash

netcat -l 8080 ;开发者_运维问答 myCommand.sh

That would do the trick, but I would like to reply to the client depending on the success or failure of the command (to have a kind of REST - like interface).

How could I do that ?

Thanks!


mkfifo foo
nc -lk 2600 0<foo | /bin/bash 1>foo

Then just nc servername 2600 and ./script.sh

kill the client with ctrl+c


The best way is to switch to the version of netcat that does support it. On Debian/Ubuntu IIRC correctly you should use netcat traditional, not netcat openbsd:

 sudo apt-get install netcat-traditional # netcat-openbsd

You will have the option of specifying explicitely which version you require:

 nc.traditional server 6767 -e myscript.sh

 nc.openbsd -l 6767

(note the subtle differences in option usage). As you can see (below) nc.traditional can be run as a standalone binary, depending on only libc and linux itself, so if you don't have installation permissions, you should be able to just drop the standalone binary somewhere (a filesystem with exec permission of course) and run it like

 /home/user/bin/mynetcat server 6767 -e myscript.sh

HTH


$ ldd `which nc.{traditional,openbsd}`

/bin/nc.traditional:
    linux-gate.so.1 =>  (0xb7888000)
    libc.so.6 => /lib/libc.so.6 (0xb7709000)
    /lib/ld-linux.so.2 (0xb7889000)
/bin/nc.openbsd:
    linux-gate.so.1 =>  (0xb77d0000)
    libglib-2.0.so.0 => /lib/libglib-2.0.so.0 (0xb76df000)
    libc.so.6 => /lib/libc.so.6 (0xb7582000)
    libpcre.so.3 => /lib/libpcre.so.3 (0xb754c000)
    /lib/ld-linux.so.2 (0xb77d1000)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜