Stop netcat as soon as grep matches something

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












1















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










share|improve this question


























    1















    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










    share|improve this question
























      1












      1








      1








      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










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 31 at 16:02









      ZermingoreZermingore

      170111




      170111




















          1 Answer
          1






          active

          oldest

          votes


















          2














          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.






          share|improve this answer























          • 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











          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%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









          2














          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.






          share|improve this answer























          • 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
















          2














          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.






          share|improve this answer























          • 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














          2












          2








          2







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















          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%2f497957%2fstop-netcat-as-soon-as-grep-matches-something%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?