Linux查看是否可以访问外网的几种方式

4,663 阅读4分钟

1、ping命令

ping是最直接的方法之一,用于测试主机与目标地址之间的连接。你可以尝试ping一个已知的外部IP地址或域名

原理: ping命令用于测试主机之间的连通性。它通过向目标主机发送 ICMP(Internet Control Message Protocol)回显请求数据包,并等待目标主机返回 ICMP 回显应答数据包来确定网络连接是否正常。如果能够收到目标主机的应答,说明可以与该主机通信,进而推断网络连接(包括访问外网)正常。

请求:

## 域名
ping -c 4 restapi.amap.com
## IP
ping -c 4 120.77.134.169

返回:

PING restapi.amap.com (120.77.134.169) 56(84) bytes of data.
64 bytes from 120.77.134.169 (120.77.134.169): icmp_seq=1 ttl=87 time=37.9 ms
64 bytes from 120.77.134.169 (120.77.134.169): icmp_seq=2 ttl=87 time=38.2 ms
64 bytes from 120.77.134.169 (120.77.134.169): icmp_seq=3 ttl=87 time=38.1 ms
64 bytes from 120.77.134.169 (120.77.134.169): icmp_seq=4 ttl=87 time=38.3 ms

--- restapi.amap.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 37.947/38.181/38.314/0.143 ms

这个命令会发送4个ICMP请求到restapi.amap.com,并显示结果。如果能够成功接收到回复,说明你的主机可以访问外部网络。

2、curl命令

原理: curl是用于在命令行中下载文件或获取网页内容的工具。如果可以成功从外网获取网页内容或者文件,那就表明终端能够访问外网。
请求域名:

curl baidu.com

返回:

<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>

请求域名2:
对于curl,可以尝试curl -I www.example.com,这个命令会获取目标网站的 HTTP 头信息。如果能够收到服务器返回的头信息,如HTTP/1.1 200 OK(状态码200表示请求成功)等内容,说明可以访问该外网网站。

curl -I baidu.com

返回:

HTTP/1.1 200 OK
Date: Tue, 26 Nov 2024 02:15:39 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Wed, 27 Nov 2024 02:15:39 GMT
Connection: Keep-Alive
Content-Type: text/html

请求:

curl http://restapi.amap.com/

返回:

{"status":"0","info":"RESOURCE_UNAVAILABLE","infocode":"10017"}

3、wget命令

wget是另一个用于下载文件的命令行工具。通过尝试下载一个文件,也可以用来测试网络连接。 请求域名:

wget baidu.com

返回:

--2024-11-26 10:17:54--  http://baidu.com/
Resolving baidu.com (baidu.com)... 39.156.66.10, 110.242.68.66
Connecting to baidu.com (baidu.com)|39.156.66.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 81 [text/html]
Saving to: ‘index.html.1

请求域名2:
对于wget,可以使用wget -S --spider www.example.com,它会模拟下载文件并检查服务器响应,而不会真正下载文件。如果返回类似Remote file exists等表示文件存在的信息,说明能够访问外网。

wget -S --spider baidu.com

返回:

Spider mode enabled. Check if remote file exists.
--2024-11-26 10:18:38--  http://baidu.com/
Resolving baidu.com (baidu.com)... 39.156.66.10, 110.242.68.66
Connecting to baidu.com (baidu.com)|39.156.66.10|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Date: Tue, 26 Nov 2024 02:18:38 GMT
  Server: Apache
  Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
  ETag: "51-47cf7e6ee8400"
  Accept-Ranges: bytes
  Content-Length: 81
  Cache-Control: max-age=86400
  Expires: Wed, 27 Nov 2024 02:18:38 GMT
  Connection: Keep-Alive
  Content-Type: text/html
Length: 81 [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving

请求:

 wget http://restapi.amap.com/

返回:

--2024-11-26 10:21:55--  http://restapi.amap.com/
Resolving restapi.amap.com (restapi.amap.com)... 120.77.134.57
Connecting to restapi.amap.com (restapi.amap.com)|120.77.134.57|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 63 [application/json]
Saving to: ‘index.html.2’

100%[========================================================================================================================================================================>] 63          --.-K/s   in 0s      

2024-11-26 10:21:55 (9.22 MB/s) - ‘index.html.2’ saved [63/63]

4、traceroute命令

traceroute命令用于追踪数据包从源计算机到目标计算机所经过的路由路径。
请求:

traceroute baidu.com

返回:

traceroute to baidu.com (39.156.66.10), 30 hops max, 60 byte packets
 1  gateway (10.31.11.1)  1.244 ms  1.883 ms  2.659 ms
 2  10.30.41.5 (10.30.41.5)  0.878 ms  0.876 ms  0.865 ms
 3  ntp.sitechcloud.com (10.30.40.1)  2.470 ms  2.459 ms  4.066 ms
 4  10.30.40.129 (10.30.40.129)  1.279 ms  1.479 ms  1.493 ms
 5  119.6.14.77 (119.6.14.77)  3.418 ms  3.398 ms  4.283 ms
 6  119.4.108.25 (119.4.108.25)  4.654 ms 119.6.14.53 (119.6.14.53)  3.187 ms 119.6.20.181 (119.6.20.181)  4.424 ms
 7  119.4.108.213 (119.4.108.213)  3.821 ms * 119.4.108.221 (119.4.108.221)  5.190 ms
 8  * * 119.6.197.177 (119.6.197.177)  4.882 ms
 9  * * 219.158.97.173 (219.158.97.173)  40.101 ms
10  219.158.96.186 (219.158.96.186)  42.674 ms 219.158.10.202 (219.158.10.202)  33.605 ms 219.158.21.182 (219.158.21.182)  37.620 ms
11  * * *
12  221.183.94.37 (221.183.94.37)  38.490 ms 221.183.94.41 (221.183.94.41)  33.988 ms  39.008 ms
13  221.183.49.122 (221.183.49.122)  37.724 ms 221.183.53.182 (221.183.53.182)  35.721 ms *
14  111.13.188.38 (111.13.188.38)  38.244 ms  34.929 ms 111.13.0.174 (111.13.0.174)  41.992 ms
15  39.156.27.5 (39.156.27.5)  36.235 ms 39.156.27.1 (39.156.27.1)  35.964 ms  38.295 ms
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  * * *
24  * * *
25  * * *
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *

5、汇总

  • 如果是主机IP,可以使用ping
  • 如果是域名,可以使用pingcurl或者wget
  • 如果是带有http协议地址,使用curlwget
  • 部分linux是没有安装telnet客户端的,所以一般测试连通性不用这个命令;
  • traceroute有种杀鸡用牛刀的感觉,只测试联通性一般也不会用;
  • 综上,使用pingcurl就足够了