Efficiently test if a port is open on Linux?

后端 未结 14 1004
面向向阳花
面向向阳花 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:30

    You can use netcat for this.

    nc ip port < /dev/null
    

    connects to the server and directly closes the connection again. If netcat is not able to connect, it returns a non-zero exit code. The exit code is stored in the variable $?. As an example,

    nc ip port < /dev/null; echo $?
    

    will return 0 if and only if netcat could successfully connect to the port.

    0 讨论(0)
  • 2020-12-02 04:33

    I wanted to check if a port is open on one of our linux test servers. I was able to do that by trying to connect with telnet from my dev machine to the test server. On you dev machine try to run:

    $ telnet test2.host.com 8080
    Trying 05.066.137.184...
    Connected to test2.host.com
    

    In this example I want to check if port 8080 is open on host test2.host.com

    0 讨论(0)
提交回复
热议问题