Scripting telnet HTTP requests
I recently needed to hit several production servers that lived behind a VIP. I had access to the servers directly but their behavior was controlled by the host that they were called with so if I used the machine name I was out of luck. Also, port numbers in the request would throw it off. So I needed to send http requests directly to the server and lie about the hostname I was using to access them.
Here is a script file that I run and pipe into telnet.
echo “open $1 $2” sleep 2 echo “GET $4 HTTP/1.0” echo “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4” echo “Host: $3” echo echo sleep 2
lets put this in a file called getpage and then we run the following
./getpage tonycode.com 80 tonycode.com /| telnet
ok. what did we just do?
getpage is sending commands on stdout and telnet is getting them via the pipe getpage 1st tells telnet to open a connection to tonycode.com ($1) port 80 ($2). getpage waits 2 seconds for the connection. Adjust as necessary. getpage sends the request. GET / HTTP/1.0 and sets the host ($3) to tonycode.com. Note $4 is the resource to fetch and we set it to /. I even threw in the user agent header for fun. Those 2 empty echo statements are necessary to tell the server this is the end of the request. Finally getpage sleeps for 2 seconds to allow time for the response to come back. Leave out this line and you'll get nada.
via: http://tonycode.com/wiki/index.php?title=Making_HTTP_requests_via_telnet