怎么在Linux服务器上测试TCP/UDP端口的连通性

翻译自:
How to Test Port[TCP/UDP] Connectivity from a Linux Server (文档 ID 2212626.1)
适用于:
Linux OS - Version Oracle Linux 5.0 to Oracle Linux 6.8 [Release OL5 to OL6U8]
Information in this document applies to any platform.
目标:
在Linux服务器上检查TCP/UDP端口的连通性。
解决方案:
telnet和nc 是用来测试端口连通性的一般工具。
telnet可以测试tcp端口的连通性。
nc可以测试tcp和udp端口的连通性。
请确保telnet和nc工具已经安装

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # yum install nc  
  2. # yum install telnet  


测试tcp端口的连通性:
语法如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. telnet <hostname/IP address> <port number>  


如下是连通成功的例子:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # telnet 192.118.20.95 22  
  2. Trying 192.118.20.95...  
  3. Connected to 192.118.20.95.  
  4. Escape character is '^]'.  
  5. SSH-2.0-OpenSSH_6.6.1  
  6.   
  7. Protocol mismatch.  
  8. Connection closed by foreign host.  

如下是连通不成功的例子:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # telnet 192.118.20.95 22  
  2. Trying 192.118.20.95...  
  3. telnet: connect to address 192.118.20.95: No route to host  


使用nc命令来测试tcp端口的连通性:
语法:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. nc -z -v <hostname/IP address> <port number>  


如下是连通成功的例子:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # nc -z -v 192.118.20.95 22  
  2. Connection to 192.118.20.95 22 port [tcp/ssh] succeeded!  


如下是连通不成功的例子:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # nc -z -v 192.118.20.95 22  
  2. nc: connect to 192.118.20.95 port 22 (tcp) failed: No route to host  


使用nc命令来测试udp端口的连通性:
语法:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. nc -z -v -u <hostname/IP address> <port number>  

 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # nc -z -v -u 192.118.20.95 123  
  2. Connection to 192.118.20.95 123 port [udp/ntp] succeeded!  


 

nc检测端口的用法

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

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


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值