9

为什么不能ping端口号?如何验证端口号可用?

 11 months ago
source link: https://www.51cto.com/article/769439.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

为什么不能ping端口号?如何验证端口号可用?

作者:HC 2023-10-11 12:59:55
Ping 不是通过端口号来测试网络连接的工具,因此,Ping 不会针对特定的端口号进行测试,而是测试目标计算机是否能够响应 ICMP 请求。

一、端口号为什么不能ping?

Ping 是一种网络工具,经常用于测试两台计算机之间的网络连接是否正常。Ping 使用 Internet 控制消息协议(ICMP)来发送 ICMP 请求消息到目标计算机,如果目标计算机正常工作并且与网络连接正常,它会回复 ICMP 响应消息。这是一种常用于测试网络可达性和测量网络延迟的工具。

32f8efa5026401bfea79946333c9140865f397.png

然而,Ping 不是通过端口号来测试网络连接的工具。它使用 ICMP 协议,而不是传统的基于端口号的协议,如传输控制协议(TCP)或用户数据报协议(UDP)。因此,Ping 不会针对特定的端口号进行测试,而是测试目标计算机是否能够响应 ICMP 请求。

综上所述,ping不能验证系统端口号是否可用,主要原因如下:

  • 协议不同:Ping 使用 ICMP 协议,而不是 TCP 或 UDP 协议。系统端口号通常与 TCP 或 UDP 协议相关联。因此,Ping 无法测试特定的端口是否处于监听状态。
  • ICMP 请求与端口号无关:Ping 发送的 ICMP 请求消息是一种用于测试网络可达性的探测工具,它只是发送一个消息给目标主机,请求一个简单的回复。它不包含与端口号相关的信息。
  • 端口是传输层概念:端口号是传输层协议(如 TCP 和 UDP)中的概念,用于区分不同的网络应用程序或服务。Ping 位于网络层,更专注于测试主机之间的可达性,而不关心传输层上的端口。

二、如何验证端口号可用?

常见的用于验证端口号是否可用的工具有telnet、curl、nc(netcat)、nmap等

使用telnet验证端口:

[root@localhost ~]# telnet 192.168.15.137 22
Trying 192.168.15.137...
Connected to 192.168.15.137.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
Connection closed by foreign host.

使用curl验证端口(还是调用telnet实现):

[root@localhost ~]# curl -v telnet://192.168.15.137:22
* About to connect() to 192.168.15.137 port 22 (#0)
*   Trying 192.168.15.137...
* Connected to 192.168.15.137 (192.168.15.137) port 22 (#0)
SSH-2.0-OpenSSH_7.4

* Send failure: Broken pipe
* Closing connection 0
curl: (55) Send failure: Broken pipe

使用curl验证端口:

[root@localhost ~]# curl 192.168.15.137:22
SSH-2.0-OpenSSH_7.4
Protocol mismatch.
curl: (56) Recv failure: Connection reset by peer

使用nc验证端口:

[root@localhost ~]# nc -zv 192.168.15.137 22
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.15.137:22.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
  • 使用nmap验证端口:
[root@localhost ~]# nmap -p 22 192.168.15.137

Starting Nmap 6.40 ( http://nmap.org ) at 2023-10-10 15:13 CST
Nmap scan report for 192.168.15.137
Host is up (0.000057s latency).
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.26 seconds

ping、telnet、curl、nc、nmap等工具功能和用途有所不同,在工作中使用的都很频繁,它们能极大地提升我们的工作效率,你学会了吗?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK