I would like to run a bash command when a remote connection is successfully established to a specified local open port. I have looked into netcat and socat, but I'm not sure if either have the functionality that I want. I do not care about the content of the communication, only that a connection has been established (the connection can be immediately dropped after starting).

This is because, I have a local resource (a Minecraft server) that I want to start only when requested. In theory, a client should attempt a connection (which will of course fail, initially), the server will then start, and after a few moments the client can connect once more to a running server.

  • 1
    portknocking works like that: a service is started (or some firewall rules are changed) only after a specific packet is sent to a specific port (and sometimes with specific content). You may get ideas from such features. – Patrick Mevzek Jun 11 at 23:00

There's the -F option to nc:

-F Pass the first connected socket using sendmsg(2) to stdout and exit. This is useful in conjunction with -X to have nc perform connection setup with a proxy but then leave the rest of the connection to another program (e.g. ssh(1) using the ssh_config(5) ProxyUseFdpass option).

I guess you could use it along this line:

nc -Fl 22222 | true && echo connection arrived

Of course you'd start the server instead of echoing. 22222 is the port and l stands for listening.

  • I'm not sure what's going on, but I actually don't have that functionality on my netcat version. On the man-page, the flags of my version go straight from -d to -h! From a quick Google search, it looks like the NetBSD version of netcat has that flag, do you have any idea how I can get it onto my system? I'm currently running Ubuntu 16.04.6 LTS – sgbrown Jun 11 at 23:25
  • 1
    @sgbrown It looks like I have a BSD version on Debian. Depending on your OS, you could just install it, or probably download the source code, compile it and then install. – Tomasz Jun 11 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.