Search code examples
linuxsocat

How to test a remote port is reachable with socat


How to test a remote port is reachable with socat?

with netcat we can

nc -zv 192.168.1.15 22

How to do that with socat?


Solution

  • As the OP requested the equivalent of -z the following socat command will ensure that it does not wait for input and just checks if the port is listening or not.

    socat /dev/null TCP:remote:port
    

    This can be used in a Docker context especially on health checks for the alpine/socat image. Here is an example that sets up a forwarder for the smtp service to a mailhog service and checks if the connection is up

    services:
      smtp:
        image: alpine/socat
        command: tcp-listen:25,fork TCP:mailhog:1025
        healthcheck: socat /dev/null TCP:mailhog:1025