0.sh
0.sh

Reputation: 2762

Connecting to an open port with bash

Is there a possible way to connect to any open port with bash ? i.e without calling any external command. :) I wanted to connect to an open port with bash without using nc or telnet.

Upvotes: 5

Views: 2521

Answers (1)

chepner
chepner

Reputation: 531165

It depends on the shell; bash, for instance, uses I/O redirection to attach to arbitrary TCP and UDP sockets. From the man page:

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.

For example, to get the current time:

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

Note that it must be a redirection; cat /dev/tcp/time-a.nist.gov/13 would only work if your file system implemented some sort of sockets-on-demand.

A very basic HTTP client:

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

Upvotes: 4

Related Questions