Extracting HTTP content in bash doesn't work with nc output












0














Let's say I have this HTTP response:



POST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

Hello


And I'm interested only in content ("Hello"). I found this command to work if the text is fed from a file:



cat data.txt | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
Hello


where data.txt contains the text above.
But if I try to feed it with the output of nc:



#!/bin/bash
while true
do
echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
done


it doesn't work, i.e. it just print out everything:



POST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

HelloPOST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

Hello


Why the piping works with cat but not with nc?










share|improve this question





























    0














    Let's say I have this HTTP response:



    POST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764

    Hello


    And I'm interested only in content ("Hello"). I found this command to work if the text is fed from a file:



    cat data.txt | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
    Hello


    where data.txt contains the text above.
    But if I try to feed it with the output of nc:



    #!/bin/bash
    while true
    do
    echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
    done


    it doesn't work, i.e. it just print out everything:



    POST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764

    HelloPOST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764

    Hello


    Why the piping works with cat but not with nc?










    share|improve this question



























      0












      0








      0







      Let's say I have this HTTP response:



      POST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      Hello


      And I'm interested only in content ("Hello"). I found this command to work if the text is fed from a file:



      cat data.txt | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
      Hello


      where data.txt contains the text above.
      But if I try to feed it with the output of nc:



      #!/bin/bash
      while true
      do
      echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
      done


      it doesn't work, i.e. it just print out everything:



      POST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      HelloPOST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      Hello


      Why the piping works with cat but not with nc?










      share|improve this question















      Let's say I have this HTTP response:



      POST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      Hello


      And I'm interested only in content ("Hello"). I found this command to work if the text is fed from a file:



      cat data.txt | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
      Hello


      where data.txt contains the text above.
      But if I try to feed it with the output of nc:



      #!/bin/bash
      while true
      do
      echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 | tr 'n' '#' | sed "s/.*##//" | tr '#' 'n'
      done


      it doesn't work, i.e. it just print out everything:



      POST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      HelloPOST / HTTP/1.1
      Content-Type: text/plain;charset=UTF-8
      Content-Length: 5
      Connection: Keep-Alive
      Accept-Encoding: gzip
      Accept-Language: en,*
      User-Agent: Mozilla/5.0
      Host: 127.0.0.1:55764

      Hello


      Why the piping works with cat but not with nc?







      bash http pipe netcat busybox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 8:38









      Cyrus

      45.3k43676




      45.3k43676










      asked Nov 18 '18 at 8:28









      MarkMark

      1,07311432




      1,07311432
























          1 Answer
          1






          active

          oldest

          votes


















          1














          output of nc goes to stderr just add & after second | to make the pipe effective:



          echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 |& tr 'n' '#' | sed "s/.*##//" | tr '#' 'n






          share|improve this answer





















          • Unfortunately it doesn't work for me. It still prints out all the stuff.
            – Mark
            Nov 18 '18 at 9:01










          • Can you please tell me your bash version? bash --version
            – gopy
            Nov 18 '18 at 9:13










          • and why you are using busybox?
            – gopy
            Nov 18 '18 at 9:14










          • GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
            – Mark
            Nov 18 '18 at 9:15






          • 1




            I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
            – gopy
            Nov 18 '18 at 9:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53359088%2fextracting-http-content-in-bash-doesnt-work-with-nc-output%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














          output of nc goes to stderr just add & after second | to make the pipe effective:



          echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 |& tr 'n' '#' | sed "s/.*##//" | tr '#' 'n






          share|improve this answer





















          • Unfortunately it doesn't work for me. It still prints out all the stuff.
            – Mark
            Nov 18 '18 at 9:01










          • Can you please tell me your bash version? bash --version
            – gopy
            Nov 18 '18 at 9:13










          • and why you are using busybox?
            – gopy
            Nov 18 '18 at 9:14










          • GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
            – Mark
            Nov 18 '18 at 9:15






          • 1




            I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
            – gopy
            Nov 18 '18 at 9:49
















          1














          output of nc goes to stderr just add & after second | to make the pipe effective:



          echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 |& tr 'n' '#' | sed "s/.*##//" | tr '#' 'n






          share|improve this answer





















          • Unfortunately it doesn't work for me. It still prints out all the stuff.
            – Mark
            Nov 18 '18 at 9:01










          • Can you please tell me your bash version? bash --version
            – gopy
            Nov 18 '18 at 9:13










          • and why you are using busybox?
            – gopy
            Nov 18 '18 at 9:14










          • GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
            – Mark
            Nov 18 '18 at 9:15






          • 1




            I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
            – gopy
            Nov 18 '18 at 9:49














          1












          1








          1






          output of nc goes to stderr just add & after second | to make the pipe effective:



          echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 |& tr 'n' '#' | sed "s/.*##//" | tr '#' 'n






          share|improve this answer












          output of nc goes to stderr just add & after second | to make the pipe effective:



          echo -e "HTTP/1.1 200 OKnn" | ./busybox-armv7l nc -l -p 55764 |& tr 'n' '#' | sed "s/.*##//" | tr '#' 'n







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 8:49









          gopygopy

          1637




          1637












          • Unfortunately it doesn't work for me. It still prints out all the stuff.
            – Mark
            Nov 18 '18 at 9:01










          • Can you please tell me your bash version? bash --version
            – gopy
            Nov 18 '18 at 9:13










          • and why you are using busybox?
            – gopy
            Nov 18 '18 at 9:14










          • GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
            – Mark
            Nov 18 '18 at 9:15






          • 1




            I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
            – gopy
            Nov 18 '18 at 9:49


















          • Unfortunately it doesn't work for me. It still prints out all the stuff.
            – Mark
            Nov 18 '18 at 9:01










          • Can you please tell me your bash version? bash --version
            – gopy
            Nov 18 '18 at 9:13










          • and why you are using busybox?
            – gopy
            Nov 18 '18 at 9:14










          • GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
            – Mark
            Nov 18 '18 at 9:15






          • 1




            I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
            – gopy
            Nov 18 '18 at 9:49
















          Unfortunately it doesn't work for me. It still prints out all the stuff.
          – Mark
          Nov 18 '18 at 9:01




          Unfortunately it doesn't work for me. It still prints out all the stuff.
          – Mark
          Nov 18 '18 at 9:01












          Can you please tell me your bash version? bash --version
          – gopy
          Nov 18 '18 at 9:13




          Can you please tell me your bash version? bash --version
          – gopy
          Nov 18 '18 at 9:13












          and why you are using busybox?
          – gopy
          Nov 18 '18 at 9:14




          and why you are using busybox?
          – gopy
          Nov 18 '18 at 9:14












          GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
          – Mark
          Nov 18 '18 at 9:15




          GNU bash, version 4.3.0(1)-release (arm-poky-linux-gnueabi). I'm using busybox because it's an embedded device and I have a limited set of commands only.
          – Mark
          Nov 18 '18 at 9:15




          1




          1




          I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
          – gopy
          Nov 18 '18 at 9:49




          I tested the solution, this works for me, the only difference is that I used busybox instead of ./busybox-armv7l . I can't guess your problem. be sure that your input(http request) doesn't contain 'rn' s
          – gopy
          Nov 18 '18 at 9:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fstackoverflow.com%2fquestions%2f53359088%2fextracting-http-content-in-bash-doesnt-work-with-nc-output%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 send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?