天天看点

Linux 故障排查-测试网络端口连通性

作者:Linux码农

1、telnet 方法

telnet 协议是 TCP/IP 协议族中的一员,是 Internet 远程登陆服务的标准协议和主要方式。

它为用户提供了在本地计算机上完成远程主机工作的能力。因此我们可以使用telnet 来测试远程机器的连通性。

telnet 软件包安装

1、检测 telnet 包是否安装

# rpm -qa | grep telnet

2、未安装,则安装 telnet 软件包

# yum install telnet

3、检测安装包是否安装成功

# rpm -qa | grep telnet

telnet-0.17-66.el7.x86_64

以上我们完成了 telnet 客户端软件的安装。

具体用法格式如下:

# telnet serverIP port

例如:

//连接一个存在的端口,连接成功
# telnet 10.198.25.126 5546
Trying 10.198.25.126...
Connected to 10.198.25.126.
Escape character is '^]'.

//连接一个不存在的端口,连接被拒绝
# telnet 10.198.25.126 1234
Trying 10.198.25.126...
telnet: connect to address 10.198.25.126: Connection refused

//连接一个不存在的网络,出现超时
# telnet 10.198.25.13 6
Trying 10.198.25.13...
telnet:connected to 10.198.25.126: Connection timed out


           

2、weg 方法

wget 是一个从网络上自动下载文件的自由工具,支持通过 HTTP、HTTPS、FTP 三个最常见的 TCP/IP 协议下载,并可以使用 HTTP 代理。

wget 名称的由来是 “World Wide Web” 与 “get” 的结合,它也可以用来测试端口的连通性。

具体用法格式如下 :

# wget ip:port

例如:

//连接一个存在的端口
# wget 1.1.1.1:6666
--2021-11-25-- http://1.1.1.1:6666/
Connecting to 1.1.1.1:6666... connected.
...

//连接一个不存在的端口
# wget 1.1.1.1:6621
--2021-11-25-- http://1.1.1.1:6621/
Connecting to 1.1.1.1:6621... failed: Connection refused.
           

3、curl 方法

curl 是利用 URL 语法在命令行方式下工作的开源文件传输工具。也可以用来测试端口的连通性。

具体用法格式如下 :

# curl ip:port

例如:

//连接一个存在的端口,输出正常信息
# curl 1.1.1.1:2010
<!DOCTYPE html>
<thml lang="en">
<head>
<title>...</title>
...

//连接一个不存在的端口,拒绝连接
# curl 1.1.1.1:2019
curl: (7) Failed connect to 1.1.1.1:2019; Connection refused
           

4、ssh 方法

ssh 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,在 linux上可以通过 ssh 命令来测试端口的连通性。

具体用法格式如下:

# ssh -v -p port username@ip
或者
# ssh -v -p port ip

例如:

# ssh -v -p 9215 1.1.1.1
OpenSSH_7.4pl, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 1.1.1.1 [1.1.1.1] port 9215.
debug1: Connection established. //出现该行表示连接成功
...


//连接一个不存在的端口
# ssh -v -p 92 1.1.1.1
ssh: connect to host 1.1.1.1 port 92: Connection refused            

5、tcping 工具方法

tcping 是一个可以替代 ping 命令来检测网络状态的一个工具。因为 ping 命令只能检测ICMP报文,当对方禁止了 ping 协议,自然 ping 命令也就无法检测了,这个时候可以通过 tcping 命令来检测网络的连通性。

tcping 有 windows 版和 linux 版本,可以根据自己的需求自行下载使用。

windows 版本下载地址

https://www.elifulkerson.com/projects/tcping.php

使用例子如下:

D:\test> tcping.exe 10.12.113.40 2543
Probing 10.12.113.40:2543/tcp - Port is open -time=4.706ms
Probing 10.12.113.40:2543/tcp - Port is open -time=0.946ms
Probing 10.12.113.40:2543/tcp - Port is open -time=0.806ms
Probing 10.12.113.40:2543/tcp - Port is open -time=1.706ms

Ping statistics for 10.12.113.40:2543
4 probes sent.
4 successful, 0 failed. <0.00% fail>
Approximate trip times in milli-seconds:
Minimum = 0.806ms, Maximum = 4.706ms, Average = 1.885ms           

linux 版下载地址

http://linuxco.de/tcping/tcping-1.3.5.tar.gz

使用例子如下:

# gcc -o tcping tcping.c
# cp tcping /usr/bin

# tcping www.baidu.com 80
www.baidu.com port 80 open.

# tcping 10.25.12.153 2335
10.25.12.153 port 2335 open
           

6、tcpdump 方法

当需要测试报文是否发送到对端,也可以使用 tcpdump 命令进行抓包分析。

通常使用的方式是通过 tcpdump 命令抓取数据包,然后通过 wireshake 工具进行分析。

使用方式如下:

//监视指定网络接口、某个具体端口号接收的tcp报文的数据包
# tcpdump -i eth0 -n tcp port 2443

//也可以指定ip, 例如获取所有210.27.48.1 的主机收到的和发出的所有数据包
# tcpdump host 210.27.48.1 

//也可以把抓取的数据包保存到cap格式的文件中,通过使用wireshark进行分析
# tcpdump tcp -i eth0 -w /test.cap
           

继续阅读