7

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?

1
  • Beware that using that '/dev/tcp' approach instead of wget or curl means you won't get all the functionality of wget or curl - like automatically handling 302 redirects, proxy server support, etc. Commented May 4, 2010 at 23:36

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried for 20 minutes, couldn't do it. Heh. You could combine the three statements into one with && if that counts as one line: exec ... && echo ... && cat ...
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.