How to test a remote port is reachable with socat

蓝咒 提交于 2021-01-21 05:46:06

问题


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?


回答1:


I would imagine that the first example in the man page would work

socat - TCP4:www.domain.org:80

For your example, this would be (and adding 2 second connect timeout):

socat - TCP4:192.168.1.15:22,connect-timeout=2

and the response would be the sshd connect banner (if that is what is listening)

SSH-2.0-OpenSSH_5.3

or

socat[12650] E connecting to AF=2 192.168.189.6:23: Connection timed out



回答2:


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


来源:https://stackoverflow.com/questions/47844060/how-to-test-a-remote-port-is-reachable-with-socat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!