Contents

检查SMTP服务器是否在Linux上运行

1. 简介

SMTP 服务器处理电子邮件的发送和接收。因此,其正常运行时间至关重要。

在本教程中,我们将研究可用于检查 SMTP 服务器是否已启动并正在运行的各种方法。

2. 使用Telnet

Telnet 是一种用于与远程主机交互的应用程序协议。我们可以使用telnet 命令来检查 SMTP 连接。在我们的例子中,我们将在本地运行一个 SMTP 服务器。因此,我们的域名是localhost

让我们使用telnet检查 SMTP 服务器是否在端口 2525 上运行:

[user@localhost ~]# telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 a42502410059 smtp4dev ready

连接在终止之前会打开一小段时间:

[user@localhost ~]# telnet localhost 2525
...
Connection closed by foreign host.

我们可以在连接打开时键入示例电子邮件,看看 SMTP 服务器是否会接收并转发它:

[user@localhost ~]# telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 5cf184409a80 smtp4dev ready
helo user.sample.com
mail from:<sender@sample.com>
rcpt to:<receiver@sample.com>
data
From: test@sample.com
Subject: This is a test
Hi there :)
How are you doing?
.
quit
250 Nice to meet you
250 New message started
250 Recipient accepted
354 End message with period
250 Mail accepted
Connection closed by foreign host.

我们看到我们的本地 SMTP 服务器收到了这封电子邮件:

Session started. Client address 10.88.0.1.
Message received. Client address 10.88.0.1. From sender@sample.com. To receiver@sample.com.
Processing received message
Processing received message DONE

相反,当 SMTP 服务器无法访问时,我们应该会看到“连接被拒绝”消息:

[user@localhost ~]# telnet localhost 2525
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

3. 使用 NetCat

nc 或ncat ( NetCat ) 命令是一个网络实用程序,用于跨网络读取和写入数据。我们可以使用这个工具来测试我们的 SMTP 服务器的连接:

[user@localhost ~]# nc -v localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to ::1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connected to 127.0.0.1:2525.
220 b9b103518a80 smtp4dev ready

nc命令**可以使用 TCP 或 UDP 两种协议中的任何一种来读取或写入数据。*TCP 协议是默认协议设置。不过,如果我们想使用 UDP 协议,我们可以简单地添加-u*选项:

[user@localhost ~]# nc -vu localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to ::1:2525.

此外,如果 SMTP 服务器不可访问,我们应该会看到“连接被拒绝”消息:

[user@localhost ~]# nc -v localhost 2525
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to ::1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connection refused.

4. 使用nmap

nmap 命令有助于网络探索。虽然该工具的目的是扫描大型网络,但它也可以扫描单个主机。需要注意的是,该工具存在法律问题——特别是关于端口扫描。因此,建议我们仅在本地使用该工具:

[user@localhost ~]# nmap localhost -p 2525 
Starting Nmap 7.70 ( https://nmap.org ) at 2021-11-11 20:38 SAST
Nmap scan report for localhost (127.0.0.1)
Host is up.
Other addresses for localhost (not scanned): ::1
PORT     STATE    SERVICE
2525/tcp filtered ms-v-worlds
Nmap done: 1 IP address (1 host up) scanned in 2.08 seconds

** STATE列的目的是描述一个端口**。该列的值是openfilteredclosedfiltered 状态意味着防火墙已到位。此外,当我们的 SMTP 服务器不可访问时,我们可以期望看到closed状态列的值:

[user@localhost ~]# nmap localhost -p 2525 
Starting Nmap 7.70 ( https://nmap.org ) at 2021-11-11 20:42 SAST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00010s latency).
Other addresses for localhost (not scanned): ::1
PORT     STATE  SERVICE
2525/tcp closed ms-v-worlds
Nmap done: 1 IP address (1 host up) scanned in 0.27 seconds