使用 bash 连接到开放端口

Connecting to an open port with bash

是否有可能使用 bash 连接到任何开放端口?即不调用任何外部命令。 :) 我想在不使用 nc 或 telnet 的情况下使用 bash 连接到开放端口,如果有人可以帮助我,我将不胜感激。

这取决于shell;例如,bash 使用 I/O 重定向附加到任意 TCP 和 UDP 套接字。来自手册页:

Bash handles several filenames specially when they are used in redirections, as
described in the following table:

    [...]

    /dev/tcp/host/port
        If host is a valid hostname or Internet address, and port is
        an integer port number or service name, bash attempts to open a
        TCP connection to  the corresponding socket.
    /dev/udp/host/port
        If host is a valid hostname or Internet address, and port is
        an integer port number or service name, bash attempts to open a
        UDP connection to the corresponding socket.

例如获取当前时间:

cat < /dev/tcp/time-a.nist.gov/13

注意必须是重定向; cat /dev/tcp/time-a.nist.gov/13 仅当您的文件系统实现了某种按需套接字时才有效。

一个非常基本的 HTTP 客户端:

$ exec 3<>/dev/tcp/www.example.com/80
$ echo "GET /" >&3
$ cat <&3