Emulating netcat -e
Asked Answered
B

2

14

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!

Barrio answered 7/6, 2011 at 17:34 Comment(1)
Could you use a named pipe for this?Republic
D
14
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

Dextrorse answered 17/11, 2011 at 2:46 Comment(0)
C
5

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)
Chauchaucer answered 7/6, 2011 at 18:49 Comment(3)
for the record, that is not what 'statically linked' means.Burstone
Yeah I know, which is why I spelled the dependencies out :) What would you call this? 'Partially statically linked', 'Mostly statically linked', 'Standalone binary'? Hmm..., perhaps the latter wouldn't be so bad! Edited, thxChauchaucer
Thanks, but I don't like the idea of distributing my little script with a 'random binary'. Plus I need to test it on a dev machine (32bits) and to deploy on a staging server (64bits).Barrio

© 2022 - 2024 — McMap. All rights reserved.