使用bash连接到开放的端口

5

有没有可能用bash连接到任何开放的端口?即不调用任何外部命令。 :) 我想使用bash连接到一个开放的端口,但不想使用nc或telnet。

1个回答

4

这取决于使用的shell;例如,bash使用I/O重定向来连接任意TCP和UDP套接字。从man页面上看:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接