版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl

Test-NetConnection概述
使用Test-NetConnection测试TCP端口时,基本语法如下:
powershell
Test-NetConnection 主机地址 -Port 端口号
其中,主机地址用于指定目标服务器,可以填写IP地址或主机名;-Port用于指定需要测试的TCP端口。
命令执行后,PowerShell会尝试与目标服务器的指定端口建立TCP连接,并返回测试结果。常见结果包含以下内容:
text
ComputerName : 目标主机
RemoteAddress : 目标IP地址
RemotePort : 目标端口
InterfaceAlias : 本地网络接口
SourceAddress : 本地IP地址
TcpTestSucceeded : True
ComputerName表示测试的目标主机,RemoteAddress表示实际连接的目标IP地址,RemotePort表示测试的目标端口,InterfaceAlias表示Windows使用的网络接口,SourceAddress表示本次连接使用的本地IP地址。
进行TCP端口测试时,主要关注TcpTestSucceeded。
text
TcpTestSucceeded : True
表示Windows能够与目标服务器的指定TCP端口建立连接。
text
TcpTestSucceeded : False
表示TCP连接没有成功建立。
Test-NetConnection测试常见端口
下面分别使用Test-NetConnection测试MySQL、Redis和RabbitMQ在Linux服务器中使用的几个端口。
本教程中使用xxx.xxx.158.246的写法隐藏了真实IP,请在测试时使用具体IP。
MySQL 3306端口
MySQL传统客户端通常使用3306端口。执行以下命令测试3306端口:
powershell
PS C:\Users\yy> Test-NetConnection xxx.xxx.158.246 -Port 3306
执行结果如下:
text
ComputerName : xxx.xxx.158.246
RemoteAddress : xxx.xxx.158.246
RemotePort : 3306
InterfaceAlias : 以太网
SourceAddress : 10.0.xxx.xxx
TcpTestSucceeded : True
Redis 6379端口
Redis默认使用6379端口。执行以下命令测试6379端口:
powershell
PS C:\Users\yy> Test-NetConnection xxx.xxx.158.246 -Port 6379
执行结果如下:
text
ComputerName : xxx.xxx.158.246
RemoteAddress : xxx.xxx.158.246
RemotePort : 6379
InterfaceAlias : 以太网
SourceAddress : 10.0.xxx.xxx
TcpTestSucceeded : True
RabbitMQ 5672端口
RabbitMQ通常使用5672端口提供应用程序连接。执行以下命令测试5672端口:
powershell
PS C:\Users\yy> Test-NetConnection xxx.xxx.158.246 -Port 5672
执行结果如下:
text
ComputerName : xxx.xxx.158.246
RemoteAddress : xxx.xxx.158.246
RemotePort : 5672
InterfaceAlias : 以太网
SourceAddress : 10.0.xxx.xxx
TcpTestSucceeded : True
RabbitMQ Management 15672端口
RabbitMQ启用Management插件后,通常使用15672端口提供Web管理界面。执行以下命令测试15672端口:
powershell
PS C:\Users\yy> Test-NetConnection xxx.xxx.158.246 -Port 15672
执行结果如下:
text
ComputerName : xxx.xxx.158.246
RemoteAddress : xxx.xxx.158.246
RemotePort : 15672
InterfaceAlias : 以太网
SourceAddress : 10.0.xxx.xxx
TcpTestSucceeded : True
以上四次测试中的TcpTestSucceeded均为True,说明当前Windows电脑能够分别建立到Linux服务器3306、6379、5672和15672端口的TCP连接。