`

Linux如何批量检测端口连通性、端口是否开放

 
阅读更多

检测端口还在用telnet?太落伍把

有没有批量检测的方法?有的。我们用nc就可以快速检测端口的开放性。

nc检测端口的用法

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

-z表示检测或者扫描端口
-w表示超时时间
-u表示使用UDP协议

例如:

 [@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 ~]#

 

端口成功联通返回值是0,提示succeeded;否则返回1,不提示任何数据

假如我们有这样一堆IP和端口。

 #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

  fi

done

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics