Efficiently test if a port is open on Linux?

后端 未结 14 1002
面向向阳花
面向向阳花 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:09
    nc -l 8000
    

    Where 8000 is the port number. If the port is free, it will start a server that you can close easily. If it isn't it will throw an error:

    nc: Address already in use
    
    0 讨论(0)
  • 2020-12-02 04:11

    Here's one that works for both Mac and Linux:

    netstat -aln | awk '$6 == "LISTEN" && $4 ~ "[\\.\:]445$"'
    
    0 讨论(0)
  • 2020-12-02 04:13

    A surprise I found out recently is that Bash natively supports tcp connections as file descriptors. To use:

    exec 6<>/dev/tcp/ip.addr.of.server/445
    echo -e "GET / HTTP/1.0\n" >&6
    cat <&6
    

    I'm using 6 as the file descriptor because 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

    As per the comment below, to test for listening on a local server in a script:

    exec 6<>/dev/tcp/127.0.0.1/445 || echo "No one is listening!"
    exec 6>&- # close output connection
    exec 6<&- # close input connection
    

    To determine if someone is listening, attempt to connect by loopback. If it fails, then the port is closed or we aren't allowed access. Afterwards, close the connection.

    Modify this for your use case, such as sending an email, exiting the script on failure, or starting the required service.

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

    they're listed in /proc/net/tcp.

    it's the second column, after the ":", in hex:

    > cat /proc/net/tcp
      sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                     
       0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 10863 1 ffff88020c785400 99 0 0 10 -1                     
       1: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7983 1 ffff88020eb7b3c0 99 0 0 10 -1                      
       2: 0500010A:948F 0900010A:2328 01 00000000:00000000 02:00000576 00000000  1000        0 10562454 2 ffff88010040f7c0 22 3 30 5 3                   
       3: 0500010A:E077 5F2F7D4A:0050 01 00000000:00000000 02:00000176 00000000  1000        0 10701021 2 ffff880100474080 41 3 22 10 -1                 
       4: 0500010A:8773 16EC97D1:0050 01 00000000:00000000 02:00000BDC 00000000  1000        0 10700849 2 ffff880104335440 57 3 18 10 -1                 
       5: 0500010A:8772 16EC97D1:0050 01 00000000:00000000 02:00000BF5 00000000  1000        0 10698952 2 ffff88010040e440 46 3 0 10 -1                  
       6: 0500010A:DD2C 0900010A:0016 01 00000000:00000000 02:0006E764 00000000  1000        0 9562907 2 ffff880104334740 22 3 30 5 4                    
       7: 0500010A:AAA4 6A717D4A:0050 08 00000000:00000001 02:00000929 00000000  1000        0 10696677 2 ffff880106cc77c0 45 3 0 10 -1  
    

    so i guess one of those :50 in the third column must be stackoverflow :o)

    look in man 5 proc for more details. and picking that apart with sed etc is left as an exercise for the gentle reader...

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

    You can use netstat this way for much faster results:

    On Linux:

    netstat -lnt | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
    

    On Mac:

    netstat -anp tcp | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
    

    This will output a list of processes listening on the port (445 in this example) or it will output nothing if the port is free.

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

    tcping is a great tool with a very low overhead.It also has a timeout argument to make it quicker:

    [root@centos_f831dfb3 ~]# tcping 10.86.151.175 22 -t 1
    10.86.151.175 port 22 open.
    [root@centos_f831dfb3 ~]# tcping 10.86.150.194 22 -t 1
    10.86.150.194 port 22 user timeout.
    [root@centos_f831dfb3 ~]# tcping 1.1.1.1 22 -t 1
    1.1.1.1 port 22 closed.
    
    0 讨论(0)
提交回复
热议问题