In order to redirect the tcp connections with bash, why the following commands return nothing?

[root@rocks7 ~]# echo -e "GET / HTTP/1.1\n\n" > /dev/tcp/yahoo.com/80
[root@rocks7 ~]# printf "GET / HTTP/1.1\n\n" > /dev/tcp/google.com/80
[root@rocks7 ~]# 
  • 1
    Because you are sending but not reading? – ajeh Apr 25 '18 at 15:51
  • If you want to receive too, install socat and try something like echo -e "GET / HTTP/1.1\n\n" | socat STDIO TCP:google.com:80 – Torin Apr 25 '18 at 16:31

You need to open the connection for read and write, using <>, and probably send a few more headers, then read the reply. For example,

exec 5<>/dev/tcp/yahoo.com/80
echo -e "GET / HTTP/1.1\r\nHost: yahoo.com\r\nContent-Length: 0\r\n\r" >&5
# get reply:   HTTP/1.1 301 Moved Permanently^M
cat <&5

I used arbitrary file descriptor 5 just to avoid clobbering the standard 0, 1, and 2. With yahoo you don't need the content length header, but other sites might not reply until they have received more input. yahoo does require the host header, or you will get the reply HTTP/1.1 400 Host Header Required.

| improve this answer | |

Adding to meuth's answer https://unix.stackexchange.com/a/440053/380936

Don't forget to close the socket after you are done talking to the remote server:

exec 5>&-

You may also want adding Connection: close, e.g.:

echo -e "GET / HTTP/1.1\r\nHost: yahoo.com\r\nContent-Length: 0\r\nConnection: close\r\n\r" >&5
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.