很多时候,我们需要测试两台或者多台机器的网络策略是否是连通状态。当然,在做系统集成时,这个就更显得尤为重要了。
【命令功能描述】
telnet 协议是 TCP/IP 协议族中的一员,是 Internet 远程登陆服务的标准协议和主要方式。
它为用户提供了在本地计算机上完成远程主机工作的能力。因此可以使用telnet 来测试远程机器的连通性。
【语法】
# 测试主机连通性
telnet IP PORT
【例子】
~ $ telnet baidu.com 80
Trying 110.242.68.66...
Connected to baidu.com.
Escape character is '^]'.
【检查】
出现 Connected
表明连接成功。如果连接不成功,也不会打印出来上面的信息。
⚠️注意:一般情况下,在生产(线上)环境,为了安全起见,我们都是要求卸载 telnet 这个工具。
【命令功能描述】
wget是一个用于从网络上下载文件的命令行工具,它可以通过指定URL来下载文件,并保存到本地计算机上。
【语法】
# 测试主机连通性
wget IP:PORT
【例子】
~ $ wget 110.242.68.66:80
--2023-08-24 17:05:42-- http://110.242.68.66/
正在连接 110.242.68.66:80... 已连接。
已发出 HTTP 请求,正在等待回应... 读取文件头错误 (Connection reset by peer)。
重试中。
【检查】
出现 已连接
表明连接成功。
【命令功能描述】
curl 是利用 URL 语法在命令行方式下工作的开源文件传输工具。也可以用来测试端口的连通性。 适合验证第三方提供的地址是否可以连通。
【语法】
# 测试主机连通性
curl IP:PORT
【例子】
~ $ curl -x 110.242.68.66:80 -v www.baidu.com
* Trying 110.242.68.66:80...
* Connected to (nil) (110.242.68.66) port 80 (#0)
> GET http://www.baidu.com/ HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.86.0
> Accept: */*
> Proxy-Connection: Keep-Alive
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 24 Aug 2023 09:15: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: Fri, 25 Aug 2023 09:15:38 GMT
< Connection: Keep-Alive
< Content-Type: text/html
<
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
* Connection #0 to host (nil) left intact
【检查】
出现 **Connected** 表明连接成功。这个比较少用,一般我们使用他下载数据包,这也是一种检测方法(今天只讨论这个)。
【命令功能描述】
它是一个用于测试网络路径的命令,可以用来查看数据从源主机到目标主机所经过的网络节点。
【语法】
traceroute --help
Version 1.4a12+Darwin
Usage: traceroute [-adDeFInrSvx] [-A as_server] [-f first_ttl] [-g gateway] [-i iface]
[-M first_ttl] [-m max_ttl] [-p port] [-P proto] [-q nqueries] [-s src_addr]
[-t tos] [-w waittime] [-z pausemsecs] host [packetlen]
【例子】
~ $ traceroute www.baidu.com
traceroute: Warning: www.baidu.com has multiple addresses; using 14.119.104.189
traceroute to www.a.shifen.com (14.119.104.189), 64 hops max, 52 byte packets
1 192.168.1.1 (192.168.1.1) 9.629 ms 1.177 ms 1.200 ms
2 116.21.40.1 (116.21.40.1) 4.135 ms 3.433 ms 4.057 ms
3 14.147.6.250 (14.147.6.250) 9.918 ms
58.62.247.245 (58.62.247.245) 7.025 ms
14.147.11.166 (14.147.11.166) 6.280 ms
【检查】
略。
【命令功能描述】
PIng是一个用于测试网络连接的命令。当我们进行Ping时,计算机会向目标IP地址发送一个数据包,并等待该地址返回响应。通过这个过程,我们可以确定网络连接的可用性和响应时间。
【语法】
# 测试主机连通性
ping IP
【例子】
~ $ ping www.baidu.com
PING www.a.shifen.com (14.119.104.254): 56 data bytes
64 bytes from 14.119.104.254: icmp_seq=0 ttl=56 time=5.401 ms
64 bytes from 14.119.104.254: icmp_seq=1 ttl=56 time=6.826 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 5.401/6.114/6.826/0.712 ms
【检查】
出现 64 bytes from 14.119.104.254: icmp_seq=0 ttl=56 time=5.401 ms
表明连接成功。停止时需要手工停止。
【命令功能描述】
当需要测试报文是否发送到对端,也可以使用 tcpdump 命令进行抓包分析。
通常使用的方式是通过 tcpdump 命令抓取数据包,然后通过wireshake 工具进行分析。
【语法】
~ $ tcpdump --help
tcpdump version 4.99.1 -- Apple version 127
libpcap version 1.10.1
LibreSSL 3.3.6
Usage: tcpdump [-AbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ] [--count]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ --immediate-mode ] [ -j tstamptype ]
[ -M secret ] [ --number ] [ --print ] [ -Q in|out|inout ]
[ -r file ] [ -s snaplen ] [ -T type ] [ --version ]
[ -V file ] [ -w file ] [ -W filecount ] [ -y datalinktype ]
[ --time-stamp-precision precision ] [ --micro ] [ --nano ]
[ -z postrotate-command ] [ -Z user ] [ expression ]
[ --apple-oneline] [ -g ]
[ --apple-md-print (metadata_arg)] [ -k (metadata_arg)]
[ --apple-pcapng] [ -P ]
[ --apple-md-filter meta-data-expression] [ -Q meta-data-expression ]
[ --apple-ext-fmt n ] [--apple-arp-plain]
[ --apple-compression n ] [--apple-truncate] [--apple-pktapv2 ]
[ --apple-head-drop ]
【命令功能描述】
tcping是一种面向连接的、可靠的、基于字节流的传输层通信协议。使用tcp协议尝试与某一个端口建立连接,然后获取与对方主机建立一次连接的回复。
ping命令可以检查网络的连通性,帮助分析和判定网络故障,是Windows、Unix和Linux系统下的一个命令。有些网络禁ping,想知道网络和端口的连通性,tcping就是一个不错的选择。而tcping是使用tcp协议尝试与某一个端口建立连接,然后获取与对方主机建立一次连接的回复。使用tcping需要下载工具到windows。
【命令功能描述】
ssh 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,在 linux上可以通过 ssh 命令来测试端口的连通性。
【语法】
# 测试主机连通性
ssh -v -p PORT Username@IP
【例子】
~ $ Aion@Macbook ~ $ ssh -v -p 80 root@baidu.com
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/Aion/.ssh/config
debug1: /Users/Aion/.ssh/config line 3: Applying options for *
debug1: /Users/Aion/.ssh/config line 47: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to baidu.com port 80.
debug1: Connection established.
debug1: identity file /Users/Aion/.ssh/id_ed25519 type 3
debug1: identity file /Users/Aion/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/Aion/.ssh/xinan_id_rsa type 0
debug1: identity file /Users/Aion/.ssh/xinan_id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
【检查】
出现 Connecting to baidu.com port 80*
表明连接成功。
其他命令也有很多,例如 mtr
、 nmap
、 nslookup
。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。