Stop netcat as soon as grep matches something
Clash Royale CLAN TAG#URR8PPP
I am facing a problem using netcat in a bash
script.
I would like to match a specific output after sending a command and continue the script execution as soon as possible (not waiting for a netcat timeout)
$> echo 'my_command' | nc -q 10 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
# ISSUE: Closes the connection quite instantly
$> echo $?
$> 1 # grep did not get (yet) the output of nc
Another try:
$> echo 'my_command' | nc -w 1 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
Binary file (standard input) matches
# ISSUE: Wait until the timeout expires
$> echo $?
$> 0
For information:
without command, netcat prints a banner message:
$>nc <IP> <PORT>
welcome message
I am not against other tools (telnet
, ...)
I would like a bash
-compliant solution.
As the expected message should come within a second I use the timeout -w 1
of nc
grep pipe netcat timeout
add a comment |
I am facing a problem using netcat in a bash
script.
I would like to match a specific output after sending a command and continue the script execution as soon as possible (not waiting for a netcat timeout)
$> echo 'my_command' | nc -q 10 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
# ISSUE: Closes the connection quite instantly
$> echo $?
$> 1 # grep did not get (yet) the output of nc
Another try:
$> echo 'my_command' | nc -w 1 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
Binary file (standard input) matches
# ISSUE: Wait until the timeout expires
$> echo $?
$> 0
For information:
without command, netcat prints a banner message:
$>nc <IP> <PORT>
welcome message
I am not against other tools (telnet
, ...)
I would like a bash
-compliant solution.
As the expected message should come within a second I use the timeout -w 1
of nc
grep pipe netcat timeout
add a comment |
I am facing a problem using netcat in a bash
script.
I would like to match a specific output after sending a command and continue the script execution as soon as possible (not waiting for a netcat timeout)
$> echo 'my_command' | nc -q 10 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
# ISSUE: Closes the connection quite instantly
$> echo $?
$> 1 # grep did not get (yet) the output of nc
Another try:
$> echo 'my_command' | nc -w 1 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
Binary file (standard input) matches
# ISSUE: Wait until the timeout expires
$> echo $?
$> 0
For information:
without command, netcat prints a banner message:
$>nc <IP> <PORT>
welcome message
I am not against other tools (telnet
, ...)
I would like a bash
-compliant solution.
As the expected message should come within a second I use the timeout -w 1
of nc
grep pipe netcat timeout
I am facing a problem using netcat in a bash
script.
I would like to match a specific output after sending a command and continue the script execution as soon as possible (not waiting for a netcat timeout)
$> echo 'my_command' | nc -q 10 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
# ISSUE: Closes the connection quite instantly
$> echo $?
$> 1 # grep did not get (yet) the output of nc
Another try:
$> echo 'my_command' | nc -w 1 <IP> <PORT> | grep -m 1 EXPECTED_OUTPUT
Binary file (standard input) matches
# ISSUE: Wait until the timeout expires
$> echo $?
$> 0
For information:
without command, netcat prints a banner message:
$>nc <IP> <PORT>
welcome message
I am not against other tools (telnet
, ...)
I would like a bash
-compliant solution.
As the expected message should come within a second I use the timeout -w 1
of nc
grep pipe netcat timeout
grep pipe netcat timeout
asked Jan 31 at 16:02
ZermingoreZermingore
170111
170111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You want to set it up so that nc
is killed as soon as grep
finishes. Here is one way:
( subshell_pid=$BASHPID ; echo 'my_command' | nc $IP $PORT > >(grep -m 1 EXPECTED_OUTPUT ; kill -13 -- -$subshell_pid ; ) )
This all runs in a subshell, and then kills all processes started by the subshell when grep
finishes.
The >()
is process substitution, which lets you pipe from one command to multiple commands.
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
add a comment |
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
);
);
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%2funix.stackexchange.com%2fquestions%2f497957%2fstop-netcat-as-soon-as-grep-matches-something%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
You want to set it up so that nc
is killed as soon as grep
finishes. Here is one way:
( subshell_pid=$BASHPID ; echo 'my_command' | nc $IP $PORT > >(grep -m 1 EXPECTED_OUTPUT ; kill -13 -- -$subshell_pid ; ) )
This all runs in a subshell, and then kills all processes started by the subshell when grep
finishes.
The >()
is process substitution, which lets you pipe from one command to multiple commands.
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
add a comment |
You want to set it up so that nc
is killed as soon as grep
finishes. Here is one way:
( subshell_pid=$BASHPID ; echo 'my_command' | nc $IP $PORT > >(grep -m 1 EXPECTED_OUTPUT ; kill -13 -- -$subshell_pid ; ) )
This all runs in a subshell, and then kills all processes started by the subshell when grep
finishes.
The >()
is process substitution, which lets you pipe from one command to multiple commands.
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
add a comment |
You want to set it up so that nc
is killed as soon as grep
finishes. Here is one way:
( subshell_pid=$BASHPID ; echo 'my_command' | nc $IP $PORT > >(grep -m 1 EXPECTED_OUTPUT ; kill -13 -- -$subshell_pid ; ) )
This all runs in a subshell, and then kills all processes started by the subshell when grep
finishes.
The >()
is process substitution, which lets you pipe from one command to multiple commands.
You want to set it up so that nc
is killed as soon as grep
finishes. Here is one way:
( subshell_pid=$BASHPID ; echo 'my_command' | nc $IP $PORT > >(grep -m 1 EXPECTED_OUTPUT ; kill -13 -- -$subshell_pid ; ) )
This all runs in a subshell, and then kills all processes started by the subshell when grep
finishes.
The >()
is process substitution, which lets you pipe from one command to multiple commands.
answered Feb 3 at 11:57
ChrisChris
1,140515
1,140515
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
add a comment |
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
Thank you very much for your answer and the SIGPIPE reminder; Your proposal is pretty much what I'm using. I upvoted and will accept your answer in a few days unless someone proposes something more 'straight forward'
– Zermingore
Feb 5 at 6:51
add a comment |
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.
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%2funix.stackexchange.com%2fquestions%2f497957%2fstop-netcat-as-soon-as-grep-matches-something%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