Extracting HTTP content in bash doesn't work with nc output
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
add a comment |
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
add a comment |
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
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
bash http pipe netcat busybox
edited Nov 18 '18 at 8:38
Cyrus
45.3k43676
45.3k43676
asked Nov 18 '18 at 8:28
MarkMark
1,07311432
1,07311432
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
|
show 4 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 4 more comments
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
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
|
show 4 more comments
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
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
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
|
show 4 more comments
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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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