Bash – Fix /dev/tcp Not Found Error

bashtcp

When I try to run the following command:

echo -e "GET / HTTP/1.1\n\n" | /dev/tcp/74.125.225.19/80

I get the following error message:

bash: /dev/tcp/74.125.225.19/80: No such file or directory

The following command works perfectly, so the problem involves how I'm using /dev/tcp:

echo -e "GET / HTTP/1.1\n\n" | nc 74.125.225.19 80

I'm in Ubuntu 13.04, so the capability should be on my system. What am I doing wrong? What are the rules for using /dev/tcp properly?

Best Answer

You have to use it in redirections:

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 the corresponding TCP socket.

So:

printf "GET / HTTP/1.1\n\n" > /dev/tcp/74.125.225.19/80

is the right way to use it.

When you used /dev/tcp/74.125.225.19/80 in a pipe, bash attempted to run a command named /dev/tcp/74.125.225.19/80 and reported an error because that file didn't exist.


The ability to handle /dev/tcp/host/port and /dev/udp/host/port in redirection was added to bash in version 2.04.

You need to compiled bash with --enable-net-redirections option.

Related Question