有没有可能使用 bash 连接到任何开放端口?即不调用任何外部命令。:) 我想使用 bash 连接到开放端口,而不使用 nc 或 telnet。
这取决于外壳;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.
Run Code Online (Sandbox Code Playgroud)
例如,要获取当前时间:
cat < /dev/tcp/time-a.nist.gov/13
Run Code Online (Sandbox Code Playgroud)
注意,必须是重定向;cat /dev/tcp/time-a.nist.gov/13仅当您的文件系统实现了某种按需套接字时才有效。
一个非常基本的 HTTP 客户端:
$ exec 3<>/dev/tcp/www.example.com/80
$ echo "GET /" >&3
$ cat <&3
Run Code Online (Sandbox Code Playgroud)