Linux 模拟 netcat -e
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6269311/
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
Emulating netcat -e
提问by user787935
How can I emulate netcat -e
with a version of netcat that does not have the -e
option ?
如何netcat -e
使用没有该-e
选项的 netcat 版本进行模拟?
I need to trigger a command remotely. I can do it with netcat - without -e
:
我需要远程触发命令。我可以用 netcat 做到这一点 - 没有-e
:
#!/bin/bash
netcat -l 8080 ; myCommand.sh
#!/bin/bash
netcat -l 8080 ; 我的命令文件
That would do the trick, butI would like to reply to the client depending on the success or failure of the command (to have a kind of REST - like interface).
这可以解决问题,但我想根据命令的成功或失败来回复客户端(拥有一种类似于 REST 的界面)。
How could I do that ?
我怎么能那样做?
Thanks!
谢谢!
回答by sehe
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:
最好的方法是切换到支持它的 netcat 版本。在 Debian/Ubuntu IIRC 上你应该正确使用 netcat Traditional,而不是 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 binarysomewhere (a filesystem with exec
permission of course) and run it like
(注意选项用法的细微差别)。正如你所看到的(下面)nc.traditional 可以作为一个独立的二进制文件运行,只依赖于 libc 和 linux 本身,所以如果你没有安装权限,你应该能够将独立的二进制文件放在某个地方(文件系统exec
当然获得许可)并像运行一样运行它
/home/user/bin/mynetcat server 6767 -e myscript.sh
HTH
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)
回答by Jocko
mkfifo foo
nc -lk 2600 0<foo | /bin/bash 1>foo
Then just nc servername 2600 and ./script.sh
然后只需 nc servername 2600 和 ./script.sh
kill the client with ctrl+c
用 ctrl+c 杀死客户端