Efficiently test if a port is open on Linux?

后端 未结 14 1055
面向向阳花
面向向阳花 2020-12-02 04:02

From a bash script how can I quickly find out whether a port 445 is open/listening on a server.

I have tried a couple of options, but I want something q

14条回答
  •  星月不相逢
    2020-12-02 04:25

    There's a very short with "fast answer" here : How to test if remote TCP port is opened from Shell script?

    nc -z  ; echo $?
    

    I use it with 127.0.0.1 as "remote" address.

    this returns "0" if the port is open and "1" if the port is closed

    e.g.

    nc -z 127.0.0.1 80; echo $?
    

    -z Specifies that nc should just scan for listening daemons, without sending any data to them. It is an error to use this option in conjunc- tion with the -l option.

提交回复
热议问题