How does Linux batch detect port connectivity and whether ports are open?

The detection port is still using telnet? too outdated

Is there a way to batch detection? some. We can use nc to quickly detect the openness of the port.

Usage of nc detection port

nc -z -w 10% IP%% PORT%

-z means to detect or scan the port
-w means the timeout time
-u means to use the UDP protocol

E.g:

 [@s136.ipcpu.com ~]# nc -z -w 10 -u 8.8.8.8 53
Connection to 8.8.8.8 53 port [udp/domain] succeeded!
[@s136.ipcpu.com ~]# nc -z -w 10 8.8.8.8 53
Connection to 8.8.8.8 53 port [tcp/domain] succeeded!
[@s136.ipcpu.com ~]# echo $?
0
[@s136.ipcpu.com ~]# nc -z -w 10 8.8.8.7 53
[@s136.ipcpu.com ~]# echo $?
1
[@s136.ipcpu.com ~]#

 

If the port is successfully connected, the return value is 0, indicating succeeded; otherwise, it returns 1, indicating no data.

Suppose we have such a bunch of IPs and ports.

 #cat test.txt
119.181.69.96 8080
119.181.118.38 8000
119.181.20.18 8080
119.181.69.37 8080

 

 

cat test.txt   |  while read line

do

 

  #nc -z -w $ 10 line

  nc -z -w 10 $line > /dev/null 2>&1

  if [ $? -eq 0 ]

  then

    echo $line:ok

  else

    echo $line:fail

  be

done

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390455&siteId=291194637