用bash连接到开放端口

时间:2015-10-02 18:09:27

标签: shell scripting

是否有可能通过bash连接到任何开放端口?即不调用任何外部命令。 :) 我想用bash连接到一个没有使用nc或telnet的开放端口,我很感激有人可以帮助我。

1 个答案:

答案 0 :(得分:2)

这取决于外壳;例如,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