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:

echo "GET http://www.google.com/ HTTP/1.1"
echo "exit"
) | telnet proxy.ip port

But it doesnt give me any output at all.

  • 1
    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
    Jan 15 '19 at 9:22
  • I do have netcat, but how would I use the proxy with it?=
    – tzippy
    Jan 15 '19 at 9:23
  • 1
    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.
    – Bodo
    Jan 15 '19 at 9:42

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

#!/usr/bin/env bash


# 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"
  # 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 :)

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.