1

I need to get the http header response from a server (google) with telnet, as i don't have curl or wget in my busybox environment. Also, I'm behind a proxy. So on the command line I can successfully do this:

$  telnet proxy.ip port
HEAD http://www.google.com/ HTTP/1.1
{hit enter twice}

HTTP/1.1 200 OK
Date: Tue, 15 Jan 2019 09:11:28 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
entire header follows...

But I dont know how to put this into a shell script. This is what I tried:

#!/bin/sh
(
echo "GET http://www.google.com/ HTTP/1.1"
echo
echo
echo "exit"
) | telnet proxy.ip port

But it doesnt give me any output at all.

3
  • 2
    I don't know if telnet will read input from a pipe. Do you have nc (netcat)? The manpage contains this example: $ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
    – Bodo
    Commented Jan 15, 2019 at 9:22
  • I do have netcat, but how would I use the proxy with it?=
    – tzippy
    Commented Jan 15, 2019 at 9:23
  • 1
    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.
    – Bodo
    Commented Jan 15, 2019 at 9:42

1 Answer 1

1

If you want to control the input and the output streams you could do this:

#!/usr/bin/env bash

proxy_host="example.proxy.local"
proxy_port="8080"
uri="http://www.google.com/"

# This is the STDIN for telnet, keep in mind that this needs to be running
# as long as the request is handled. You can test the result by removing the
# sleep.
send_headers () {
  # Note the use of HTTP/1.0. 1.1 would keep the connection open
  # and you will need to wait for the 10 second timout below.
  echo "GET $uri HTTP/1.0"
  echo
  echo
  # 10 second timeout to keep STDIN open
  sleep 10
}

# This reads the telnet output per line, do your stuff here
while read -r line; do
  echo "$line"
done < <(telnet "$proxy_host" "$proxy_port" < <(send_headers))

The command1 < <(command2) syntax is just a reversed pipe (same as command2 | command1). In this example it connects the STDOUT file descriptor of command2 to the STDIN file descriptor of command1. The downside of this is that all 'commands' in this pipeline need to be running for it to work. If you remove the sleep, the telnet command is ended prematurely. The good news is that, when you use HTTP/1.0, the connection is closed for you, and the pipe is neatly closed, without waiting for the 10 second send_headers timeout.

NB: I don't have a proxy to test the code so I hope it works for you :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .