Efficiently test if a port is open on Linux?

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

    nmap is the right tool. Simply use nmap example.com -p 80

    You can use it from local or remote server. It also helps you identify if a firewall is blocking the access.

    0 讨论(0)
  • 2020-12-02 04:16
    ss -tl4 '( sport = :22 )'
    

    2ms is quick enough ?

    Add the colon and this works on Linux

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

    You can use netcat command as well

    [location of netcat]/netcat -zv [ip] [port]
    

    or

    nc -zv [ip] [port]
    

    -z – sets nc to simply scan for listening daemons, without actually sending any data to them.
    -v – enables verbose mode.

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

    Based on Spencer Rathbun's answer, using bash:

    true &>/dev/null </dev/tcp/127.0.0.1/$PORT && echo open || echo closed
    
    0 讨论(0)
  • 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 <host> <port>; 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.

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

    If you're using iptables try:

    iptables -nL
    

    or

    iptables -nL | grep 445
    
    0 讨论(0)
提交回复
热议问题