Telnet 是一种用于远程访问计算机的网络协议,并提供基于文本的双向通信。因此,需要Telnet服务器和客户端进行通信。
Telnet 是流行的 Linux/Windows 实用程序之一,长期以来一直发挥着其作用。
现代系统上 Telnet 的主要问题是它不安全。所有与 Telnet 的通信均以纯文本形式进行,并且所有网络流量均未加密。本质上,任何拥有适当访问权限和工具的人都可以窥探和读取您的网络流量。因此,大多数现代Linux操作系统都没有预装Telnet,其他操作系统也不建议使用Telnet。
随着 SSH 或 Secure Shell 协议(它不仅仅是 Telnet 的加密替代品)的出现,Telnet 的原始用途早已过时。然而,许多系统管理员和技术爱好者至今仍在使用 Telnet 的另一个用途:检查远程 TCP 端口的连接。
您可以使用 telnet 命令轻松检查远程 TCP 端口是否正在正确侦听和响应。下面的代码片段展示了如何通过检查HTTP/HTTPS连接来检查google.com是否已启动。
$ telnet google.com 80
Trying 142.250.183.206...
Connected to google.com.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
$
$ telnet google.com 443
Trying 142.250.183.206...
Connected to google.com.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
$使用telnet检查时,未打开或无法访问的 TCP 端口的行为如下:
$ telnet google.com 22
Trying 142.250.193.174...
^C
$这使得结合ping 、 traceroute或tracepath以及netstat等命令可以轻松地解决简单的网络连接问题。
如果您使用的是 RHEL 8(或旧版本的 RHEL/CentOS),则可以选择使用 nc(或 Ncat 或 Network Connector),它支持许多与网络诊断相关的选项。了解如何在 RHEL8 和类似系统上安装和使用此工具。
什么是数控?
nc(或 Ncat)是一种流行的通用命令行工具,用于读取、写入、重定向和加密网络上的数据。虽然最初是为nmap项目创建的,但现在可以使用多个 Netcat 实现。它通过适用于 IPv4 和 IPv6 的 TCP 和 UDP 运行,提供无限的潜在用例。
以下是nc实用程序的一些主要功能。
- 链接
ncats能力 - 将 TCP、UDP 和 SCTP 端口重定向到其他站点
- 支持 SSL 的通信加密
- 通过 SOCK4/5 或 HTTP 代理的代理支持(包括身份验证)
- 支持多种平台,包括 Windows、Linux 和 macOS
数控安装
nc作为 RHEL 系统上默认存储库的一部分提供。要在 RHEL 7 系统上安装,只需在终端中发出以下命令:
$ sudo yum install -y nc对于 RHEL 8 系统, dnf可以使用如下:
$ sudo dnf install -y nc检查 TCP 连接
nc提供了许多支持跨应用程序的许多用例的功能,但一个常见的功能是在网络故障排除期间替代telnet 。
显示nc TCP端口是否可达。语法是:
$ nc -vz <IP/DNS> <Port>举个例子,假设您想检查是否可以通过http或https访问。您可以使用nc进行检查,如下所示(端口80用于http ,端口443用于https )。
$ nc -vz .com 80
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 104.26.11.88:80.
Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.
$
$ nc -vz .com 443
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 104.26.10.88:443.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
$同样,无法访问或被阻止的端口将显示如下输出(检查多个地址,因为 DNS 指向多个 IP):
$ nc -vz .com 22
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to 172.67.70.213 failed: Connection timed out.
Ncat: Trying next address...
Ncat: Connection to 104.26.11.88 failed: Connection timed out.
Ncat: Trying next address...
Ncat: Connection to 104.26.10.88 failed: Connection timed out.
Ncat: Trying next address...
Ncat: Connection to 2606:4700:20::681a:a58 failed: Network is unreachable.
Ncat: Trying next address...
Ncat: Connection to 2606:4700:20::681a:b58 failed: Network is unreachable.
Ncat: Trying next address...
Ncat: Network is unreachable.
$
$ dig .com +short
104.26.10.88
172.67.70.213
104.26.11.88
$检查 UDP 连接
telnet只能看到与远程TCP端口的通信,而nc可以看到TCP和UDP连接。
您可以使用nc发送 UDP 数据包而不是默认的 TCP 数据包。
$ nc -vzu <IP/DNS> <Port>然而,与 TCP 不同的是,UDP 是一种无会话协议,因此除非远程端有监听进程发送 UDP 数据包,否则仅在一端发送 UDP 数据包并不能在所有可能的情况下提供端到端的覆盖。检查 UDP 连接。如果有任何响应, nc就无法确定它发送的数据包是否已到达目的地。但是,假设您可以正确访问远程服务器上的 CLI, nc提供了另一种方法来启动 UDP 侦听器来确定端到端 UDP 连接。
因此,假设您需要使用nc检查两个 Linux 主机之间的 UDP 连接以获取 DNS,一个简单的方法是启动一个nc服务器来侦听所需的端口。
$ sudo nc -ul <Port>对于 DNS,您需要检查端口53 。这使得上面的命令看起来像这样:
$ nc -ul 53在客户端,我们需要启动另一个nc进程,将 UDP 数据包发送到服务器。
$ nc -u <IP/DNS> <Port>这将创建以下命令:
$ nc -u <IP/DNS> 53考虑到没有任何东西阻止这两台机器之间端口53上的 UDP 流量,您在一台机器上键入的任何内容也应该对另一台主机可见,就像双向聊天一样。如果没有,您的防火墙将阻止这两个系统之间的连接。
带有nc服务器和客户端模型非常适合主机之间的这种简单连接检查。与上面的UDP检查类似, nc也可以监听特定端口上的TCP数据包。
$ sudo nc -l <Port>在客户端,您通常可以通过发送 TCP 数据包来验证连接。
$ nc <IP/DNS> <Port>对于 TCP 连接,不需要上述服务器/客户端nc方法,因为它是面向连接的协议并且基于确认进行操作(与 UDP 不同)。所有在 TCP 上运行的侦听进程都直接响应nc TCP 数据包。
概括
本文介绍了nc实用程序在检查端口连接时如何直接替代现代 Linux 系统上的telnet ,从而在诊断和解决网络问题时为最终用户提供更多功能。
可以使用nc -h命令访问nc帮助。
$ nc -h
Ncat 7.70 ( https://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]
Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
-4 Use IPv4 only
-6 Use IPv6 only
-U, --unixsock Use Unix domain sockets only
-C, --crlf Use CRLF for EOL sequence
-c, --sh-exec <command> Executes the given command via /bin/sh
-e, --exec <command> Executes the given command
--lua-exec <filename> Executes the given Lua script
-g hop1[,hop2,...] Loose source routing hop points (8 max)
-G <n> Loose source routing hop pointer (4, 8, 12, ...)
-m, --max-conns <n> Maximum <n> simultaneous connections
-h, --help Display this help screen
-d, --delay <time> Wait between read/writes
-o, --output <filename> Dump session data to a file
-x, --hex-dump <filename> Dump session data as hex to a file
-i, --idle-timeout <time> Idle read/write timeout
-p, --source-port port Specify source port to use
-s, --source addr Specify source address to use (doesn't affect -l)
-l, --listen Bind and listen for incoming connections
-k, --keep-open Accept multiple connections in listen mode
-n, --nodns Do not resolve hostnames via DNS
-t, --telnet Answer Telnet negotiations
-u, --udp Use UDP instead of default TCP
--sctp Use SCTP instead of default TCP
-v, --verbose Set verbosity level (can be used several times)
-w, --wait <time> Connect timeout
-z Zero-I/O mode, report connection status only
--append-output Append rather than clobber specified output files
--send-only Only send data, ignoring received; quit on EOF
--recv-only Only receive data, never send anything
--allow Allow only given hosts to connect to Ncat
--allowfile A file of hosts allowed to connect to Ncat
--deny Deny given hosts from connecting to Ncat
--denyfile A file of hosts denied from connecting to Ncat
--broker Enable Ncat's connection brokering mode
--chat Start a simple Ncat chat server
--proxy <addr[:port]> Specify address of host to proxy through
--proxy-type <type> Specify proxy type ("http" or "socks4" or "socks5")
--proxy-auth <auth> Authenticate with HTTP or SOCKS proxy server
--ssl Connect or listen with SSL
--ssl-cert Specify SSL certificate file (PEM) for listening
--ssl-key Specify SSL private key (PEM) for listening
--ssl-verify Verify trust and domain name of certificates
--ssl-trustfile PEM file containing trusted SSL certificates
--ssl-ciphers Cipherlist containing SSL ciphers to use
--ssl-alpn ALPN protocol list to use.
--version Display Ncat's version information and exit
See the ncat(1) manpage for full options, descriptions and usage examples
$有关nc命令的更多信息,请参阅其手册页。
$ man nc



![2021 年如何设置 Raspberry Pi Web 服务器 [指南]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)

