Get HTTP header response with telnet in shell script using a proxy

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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.










share|improve this question



















  • 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.0rnrn" | nc host.example.com 80

    – Bodo
    Jan 15 at 9:22











  • I do have netcat, but how would I use the proxy with it?=

    – tzippy
    Jan 15 at 9:23






  • 1





    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

    – Bodo
    Jan 15 at 9:42















0















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.










share|improve this question



















  • 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.0rnrn" | nc host.example.com 80

    – Bodo
    Jan 15 at 9:22











  • I do have netcat, but how would I use the proxy with it?=

    – tzippy
    Jan 15 at 9:23






  • 1





    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

    – Bodo
    Jan 15 at 9:42













0












0








0








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.










share|improve this question
















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.







shell-script http telnet






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 9:50









Rui F Ribeiro

39.8k1479132




39.8k1479132










asked Jan 15 at 9:15









tzippytzippy

15116




15116







  • 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.0rnrn" | nc host.example.com 80

    – Bodo
    Jan 15 at 9:22











  • I do have netcat, but how would I use the proxy with it?=

    – tzippy
    Jan 15 at 9:23






  • 1





    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

    – Bodo
    Jan 15 at 9:42












  • 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.0rnrn" | nc host.example.com 80

    – Bodo
    Jan 15 at 9:22











  • I do have netcat, but how would I use the proxy with it?=

    – tzippy
    Jan 15 at 9:23






  • 1





    Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

    – Bodo
    Jan 15 at 9:42







1




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.0rnrn" | nc host.example.com 80

– Bodo
Jan 15 at 9:22





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.0rnrn" | nc host.example.com 80

– Bodo
Jan 15 at 9:22













I do have netcat, but how would I use the proxy with it?=

– tzippy
Jan 15 at 9:23





I do have netcat, but how would I use the proxy with it?=

– tzippy
Jan 15 at 9:23




1




1





Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

– Bodo
Jan 15 at 9:42





Replace nc host.example.com 80 with nc proxy.ip port in the example from the manpage.

– Bodo
Jan 15 at 9:42










1 Answer
1






active

oldest

votes


















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






share|improve this answer
























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494558%2fget-http-header-response-with-telnet-in-shell-script-using-a-proxy%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









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






    share|improve this answer





























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






      share|improve this answer



























        1












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






        share|improve this answer















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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 15 at 10:30

























        answered Jan 15 at 10:12









        JohanJohan

        2565




        2565



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494558%2fget-http-header-response-with-telnet-in-shell-script-using-a-proxy%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?