Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using /dev/tcp instead of wget

Tags:

linux

bash

wget

Why does this work:

exec 3<>/dev/tcp/www.google.com/80
echo -e "GET / HTTP/1.1\n\n">&3
cat <&3

And this fail:

echo -e "GET / HTTP/1.1\n\n" > /dev/tcp/www.google.com/80
cat </dev/tcp/www.google.com/80

Is there a way to do it in one-line w/o using wget, curl, or some other library?

like image 545
User1 Avatar asked May 04 '10 23:05

User1


2 Answers

The second snippet fails because it opens two separate TCP sockets. The echo connects to www.google.com and writes the HTTP request; and then the second line opens another connection and tries to read from that socket. The second socket simply blocks because Google is waiting for the HTTP request to be sent.

like image 114
John Kugelman Avatar answered Oct 25 '22 00:10

John Kugelman


Not my area of expertise, but I think that the second sample will open a second connection, while the first sample keeps an open handle to the same connection. So any solution which involves opening only one connection should work.

like image 25
Lucero Avatar answered Oct 25 '22 00:10

Lucero