Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to website without curl or bash

Tags:

bash

shell

curl

get

I'm trying to connect to a website like this "examplesite.com:9000/link" using a method like this:

echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1

I've seen people ping google with the above code. I can use curl or wget to go to that site but I don't wanna use those methods because I'm using a microcontroller that doesn't support curl or wget.

Could someone explain how the above code is working?

like image 635
Taimoor Khan Avatar asked Nov 02 '15 19:11

Taimoor Khan


1 Answers

  1. nc opens a connection to port 80 on google.com
  2. The echo statement is a valid GET request, using HTTP/1.0 protocol
  3. > /dev/null 2>&1 redirects both stdout and stderr, producing no output
  4. You can tell success by the exit code, in $? (value of 0 means success)

You could write this shorter:

echo -e "GET /\n\n" | nc google.com 80

And more portable (without echo -e):

printf "GET /\n\n" | nc google.com 80

Or more portable but still with echo:

{ echo GET /; echo; } | nc google.com 80
like image 161
janos Avatar answered Sep 30 '22 08:09

janos