telnet
命令是最受欢迎的网络故障排除工具之一,适用于从系统管理员到网络爱好者的任何人。在网络计算的早期,telnet
用于连接到远程系统。你可以使用 telnet
访问远程系统上的端口,登录并在该主机上运行命令。
由于 telnet 缺乏加密,它在很大程度上已被 OpenSSL 取代。然而,telnet 的相关性仍然存在(在某些情况下甚至今天仍然存在),作为一种智能的 ping
。虽然 ping
命令是探测主机响应性的好方法,但它只能做到这一点。另一方面,Telnet 不仅可以确认活动的端口,还可以与该端口上的服务进行交互。即便如此,由于大多数现代网络服务都已加密,因此 telnet 的用处可能会大大降低,具体取决于您要实现的目标。
OpenSSL s_client
对于曾经需要 telnet 的大多数任务,我现在使用 OpenSSL 的 s_client
命令。(对于某些任务我使用 curl
,但这些情况无论如何我也不会使用 telnet。)大多数人知道 OpenSSL 是一个用于加密的库和框架,但并非所有人都意识到它也是一个命令。openssl
命令的 s_client
组件实现了一个通用的 SSL 或 TLS 客户端,帮助您使用 SSL 或 TLS 连接到远程主机。它旨在用于测试,并且至少在内部使用与库相同的功能。
安装 OpenSSL
您的 Linux 系统可能已经安装了 OpenSSL。如果未安装,您可以使用发行版的软件包管理器安装它
$ sudo dnf install openssl
在 Debian 或类似系统上
$ sudo apt install openssl
安装完成后,验证它是否按预期响应
$ openssl version
OpenSSL x.y.z FIPS
验证端口访问
最基本的 telnet 用法是这样的任务
$ telnet mail.example.com 25
Trying 98.76.54.32...
Connected to example.com.
Escape character is '^]'.
这将打开一个与(在本例中)在端口 25 上侦听的任何服务(可能是邮件服务器)的交互式会话。只要您获得访问权限,就可以与该服务进行通信。
如果端口 25 不可访问,则连接将被拒绝。
OpenSSL 类似,但通常交互性较差。要验证端口访问
$ openssl s_client -connect example.com:80
CONNECTED(00000003)
140306897352512:error:1408F10B:SSL [...]
no peer certificate available
No client certificate CA names sent
SSL handshake has read 5 bytes and written 309 bytes
Verification: OK
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
这只不过是一个有针对性的 ping。正如您从输出中看到的那样,没有交换 SSL 证书,因此连接立即终止。要充分利用 openssl s_client
,您必须以加密端口为目标。
交互式 OpenSSL
Web 浏览器和 Web 服务器进行交互,使得定向到端口 80 的流量实际上被转发到 443,即为加密 HTTP 流量保留的端口。了解这一点后,您可以使用 openssl
命令导航到加密端口,并与在其上运行的任何 Web 服务进行交互。
首先,使用 SSL 连接到端口。使用 -showcerts
选项会导致 SSL 证书打印到您的终端,使初始输出比 telnet 更详细
$ openssl s_client -connect example.com:443 -showcerts
[...]
0080 - 52 cd bd 95 3d 8a 1e 2d-3f 84 a0 e3 7a c0 8d 87 R...=..-?...z...
0090 - 62 d0 ae d5 95 8d 82 11-01 bc 97 97 cd 8a 30 c1 b.............0.
00a0 - 54 78 5c ad 62 5b 77 b9-a6 35 97 67 65 f5 9b 22 Tx\.b[w..5.ge.."
00b0 - 18 8a 6a 94 a4 d9 7e 2f-f5 33 e8 8a b7 82 bd 94 ..j...~/.3......
Start Time: 1619661100
Timeout : 7200 (sec)
Verify return code: 0 (ok)
Extended master secret: no
Max Early Data: 0
-
read R BLOCK
您将进入交互式会话。最终,此会话将关闭,但如果您及时操作,您可以向服务器发送 HTTP 信号
[...]
GET / HTTP/1.1
HOST: example.com
按 Return 键两次,您将收到 example.com/index.html
的数据
[...]
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
邮件服务器
您还可以使用 OpenSSL 的 s_client
来测试加密的邮件服务器。为此,您必须将测试用户的用户名和密码编码为 Base64。
这是一个简单的方法
$ perl -MMIME::Base64 -e 'print encode_base64("username");'
$ perl -MMIME::Base64 -e 'print encode_base64("password");'
记录这些值后,您可以通过 SSL 连接到邮件服务器,通常在端口 587 上
$ openssl s_client -starttls smtp \
-connect email.example.com:587
> ehlo example.com
> auth login
##paste your user base64 string here##
##paste your password base64 string here##
> mail from: noreply@example.com
> rcpt to: admin@example.com
> data
> Subject: Test 001
This is a test email.
.
> quit
检查您的电子邮件(在本示例代码中,它是 admin@example.com
)中来自 noreply@example.com
的测试消息。
OpenSSL 还是 telnet?
telnet 仍然有一些用途,但它不再是曾经不可或缺的工具。该命令已被许多发行版降级为“旧版”网络软件包,但由于没有 telnet-ng
或其他明显的替代品,管理员有时会感到困惑,为什么它被排除在默认安装之外。答案是它不再是必需的,它变得越来越没用——这是好事。网络安全很重要,因此请习惯使用与加密接口交互的工具,这样您就不必在故障排除期间禁用安全措施。
6 条评论