Setting variable output from timeoutSetting a variable whose value depends on another variable“Expect” automation tool output to shell variableTimeout function return valueNFS caches expiring unexpectedlytimeout command and pipe - order of precedenceSetting jq output to a Bash VariableSetting Bash variable with osascript always incorrect.sed - calling a variable from a file with multilineTimeout command breaks command that expects inputWhere is TMOUT being set in CentOS 7? How can I disable it?

when is out of tune ok?

Sequence of Tenses: Translating the subjunctive

What is the difference between "behavior" and "behaviour"?

Opposite of a diet

How do scammers retract money, while you can’t?

Method to test if a number is a perfect power?

Return the Closest Prime Number

Type int? vs type int

How to check is there any negative term in a large list?

How does buying out courses with grant money work?

Two monoidal structures and copowering

How can a function with a hole (removable discontinuity) equal a function with no hole?

Purchasing a ticket for someone else in another country?

Customer Requests (Sometimes) Drive Me Bonkers!

How do I go from 300 unfinished/half written blog posts, to published posts?

Is there a problem with hiding "forgot password" until it's needed?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Is there a korbon needed for conversion?

Roman Numeral Treatment of Suspensions

What can we do to stop prior company from asking us questions?

Why not increase contact surface when reentering the atmosphere?

Do sorcerers' Subtle Spells require a skill check to be unseen?

Escape a backup date in a file name

How does Loki do this?



Setting variable output from timeout


Setting a variable whose value depends on another variable“Expect” automation tool output to shell variableTimeout function return valueNFS caches expiring unexpectedlytimeout command and pipe - order of precedenceSetting jq output to a Bash VariableSetting Bash variable with osascript always incorrect.sed - calling a variable from a file with multilineTimeout command breaks command that expects inputWhere is TMOUT being set in CentOS 7? How can I disable it?













2















I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a conditional statement later. The complication is the link to the target device may not be up, and therefore the script could hang indefinitely if not killed via a timeout.



This sets output to what I'd expect, but does not handle the link being down:



output=$(telnet 1.2.3.4 1234 | grep "something")


This will spit out the expected output while handling the link being down:



timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something"


I can even direct the output to a file and the file will contain the output:



timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" > /tmp/tmpfile.txt


Unfortunately rapid write/read of a file like this isn't an option because of how much it'll fill up log files.



But, when I try to combine everything, the variable doesn't set:



output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something")


Or, rather, it sets it to a blank value, because if I set it before running the above, the variable is blank afterwards.










share|improve this question


























    2















    I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a conditional statement later. The complication is the link to the target device may not be up, and therefore the script could hang indefinitely if not killed via a timeout.



    This sets output to what I'd expect, but does not handle the link being down:



    output=$(telnet 1.2.3.4 1234 | grep "something")


    This will spit out the expected output while handling the link being down:



    timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something"


    I can even direct the output to a file and the file will contain the output:



    timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" > /tmp/tmpfile.txt


    Unfortunately rapid write/read of a file like this isn't an option because of how much it'll fill up log files.



    But, when I try to combine everything, the variable doesn't set:



    output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something")


    Or, rather, it sets it to a blank value, because if I set it before running the above, the variable is blank afterwards.










    share|improve this question
























      2












      2








      2








      I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a conditional statement later. The complication is the link to the target device may not be up, and therefore the script could hang indefinitely if not killed via a timeout.



      This sets output to what I'd expect, but does not handle the link being down:



      output=$(telnet 1.2.3.4 1234 | grep "something")


      This will spit out the expected output while handling the link being down:



      timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something"


      I can even direct the output to a file and the file will contain the output:



      timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" > /tmp/tmpfile.txt


      Unfortunately rapid write/read of a file like this isn't an option because of how much it'll fill up log files.



      But, when I try to combine everything, the variable doesn't set:



      output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something")


      Or, rather, it sets it to a blank value, because if I set it before running the above, the variable is blank afterwards.










      share|improve this question














      I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a conditional statement later. The complication is the link to the target device may not be up, and therefore the script could hang indefinitely if not killed via a timeout.



      This sets output to what I'd expect, but does not handle the link being down:



      output=$(telnet 1.2.3.4 1234 | grep "something")


      This will spit out the expected output while handling the link being down:



      timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something"


      I can even direct the output to a file and the file will contain the output:



      timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" > /tmp/tmpfile.txt


      Unfortunately rapid write/read of a file like this isn't an option because of how much it'll fill up log files.



      But, when I try to combine everything, the variable doesn't set:



      output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something")


      Or, rather, it sets it to a blank value, because if I set it before running the above, the variable is blank afterwards.







      bash variable telnet timeout






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Ken OhKen Oh

      102127




      102127




















          2 Answers
          2






          active

          oldest

          votes


















          1














          telnet expects a tty on its stdin, but timeout takes that away.



          If you really insist on using telnet you may do so by adding --foreground option to timeout, as in:



          output=$(timeout --foreground --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something")


          Besides, if you can have nc on your system you should really rather use that for your purpose:



          output=$(timeout 3 nc 1.2.3.4 1234 | grep "something")


          If neither nc nor timeout --foreground are an option for you then you really need an alternative to telnet that won’t need a tty.



          I see you tagged your question bash, so you could use Bash’s own networking facilities, and thus your line might become like:



          output=$(timeout 3 cat < /dev/tcp/1.2.3.4/1234 | grep "something")


          If not even cat is an option then you could replace it with a one-liner script in Bash, like in:



          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/1.2.3.4/1234 | grep "something"')


          Hoping that at least stdbuf (which is part of standard coreutils package) is available in your system.



          In this last alternative however pay attention to your grep regex: if you have single-quotes in there then you need to escape them by first quitting the main single-quote pair.



          That is needed also if you need to pass variables (eg hostname and/or port number) from your shell to the one-liner script. For instance:



          hostname=1.2.3.4
          portnumber=1234

          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/'"$hostname"'/'"$portnumber"' | grep "something"')


          Here I'm assuming that $hostname and $portnumber values can be trusted, ie provided by you or by other trusted sources that won't give illegitimate, invalid, or dangerous values.






          share|improve this answer

























          • Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

            – Ken Oh
            23 hours ago











          • @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

            – LL3
            16 hours ago











          • Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

            – Ken Oh
            13 hours ago






          • 1





            @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

            – LL3
            11 hours ago











          • I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

            – Ken Oh
            11 hours ago



















          1














          It is the standard error that is not being redirected (when your link is down)!
          This will solve your problem, because it redirects the standard error to the standard output.



          output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something") 





          share|improve this answer























          • I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

            – Ken Oh
            yesterday












          • Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

            – Luciano Andress Martini
            yesterday












          • Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

            – Ken Oh
            yesterday











          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%2f508806%2fsetting-variable-output-from-timeout%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          telnet expects a tty on its stdin, but timeout takes that away.



          If you really insist on using telnet you may do so by adding --foreground option to timeout, as in:



          output=$(timeout --foreground --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something")


          Besides, if you can have nc on your system you should really rather use that for your purpose:



          output=$(timeout 3 nc 1.2.3.4 1234 | grep "something")


          If neither nc nor timeout --foreground are an option for you then you really need an alternative to telnet that won’t need a tty.



          I see you tagged your question bash, so you could use Bash’s own networking facilities, and thus your line might become like:



          output=$(timeout 3 cat < /dev/tcp/1.2.3.4/1234 | grep "something")


          If not even cat is an option then you could replace it with a one-liner script in Bash, like in:



          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/1.2.3.4/1234 | grep "something"')


          Hoping that at least stdbuf (which is part of standard coreutils package) is available in your system.



          In this last alternative however pay attention to your grep regex: if you have single-quotes in there then you need to escape them by first quitting the main single-quote pair.



          That is needed also if you need to pass variables (eg hostname and/or port number) from your shell to the one-liner script. For instance:



          hostname=1.2.3.4
          portnumber=1234

          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/'"$hostname"'/'"$portnumber"' | grep "something"')


          Here I'm assuming that $hostname and $portnumber values can be trusted, ie provided by you or by other trusted sources that won't give illegitimate, invalid, or dangerous values.






          share|improve this answer

























          • Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

            – Ken Oh
            23 hours ago











          • @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

            – LL3
            16 hours ago











          • Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

            – Ken Oh
            13 hours ago






          • 1





            @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

            – LL3
            11 hours ago











          • I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

            – Ken Oh
            11 hours ago
















          1














          telnet expects a tty on its stdin, but timeout takes that away.



          If you really insist on using telnet you may do so by adding --foreground option to timeout, as in:



          output=$(timeout --foreground --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something")


          Besides, if you can have nc on your system you should really rather use that for your purpose:



          output=$(timeout 3 nc 1.2.3.4 1234 | grep "something")


          If neither nc nor timeout --foreground are an option for you then you really need an alternative to telnet that won’t need a tty.



          I see you tagged your question bash, so you could use Bash’s own networking facilities, and thus your line might become like:



          output=$(timeout 3 cat < /dev/tcp/1.2.3.4/1234 | grep "something")


          If not even cat is an option then you could replace it with a one-liner script in Bash, like in:



          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/1.2.3.4/1234 | grep "something"')


          Hoping that at least stdbuf (which is part of standard coreutils package) is available in your system.



          In this last alternative however pay attention to your grep regex: if you have single-quotes in there then you need to escape them by first quitting the main single-quote pair.



          That is needed also if you need to pass variables (eg hostname and/or port number) from your shell to the one-liner script. For instance:



          hostname=1.2.3.4
          portnumber=1234

          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/'"$hostname"'/'"$portnumber"' | grep "something"')


          Here I'm assuming that $hostname and $portnumber values can be trusted, ie provided by you or by other trusted sources that won't give illegitimate, invalid, or dangerous values.






          share|improve this answer

























          • Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

            – Ken Oh
            23 hours ago











          • @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

            – LL3
            16 hours ago











          • Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

            – Ken Oh
            13 hours ago






          • 1





            @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

            – LL3
            11 hours ago











          • I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

            – Ken Oh
            11 hours ago














          1












          1








          1







          telnet expects a tty on its stdin, but timeout takes that away.



          If you really insist on using telnet you may do so by adding --foreground option to timeout, as in:



          output=$(timeout --foreground --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something")


          Besides, if you can have nc on your system you should really rather use that for your purpose:



          output=$(timeout 3 nc 1.2.3.4 1234 | grep "something")


          If neither nc nor timeout --foreground are an option for you then you really need an alternative to telnet that won’t need a tty.



          I see you tagged your question bash, so you could use Bash’s own networking facilities, and thus your line might become like:



          output=$(timeout 3 cat < /dev/tcp/1.2.3.4/1234 | grep "something")


          If not even cat is an option then you could replace it with a one-liner script in Bash, like in:



          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/1.2.3.4/1234 | grep "something"')


          Hoping that at least stdbuf (which is part of standard coreutils package) is available in your system.



          In this last alternative however pay attention to your grep regex: if you have single-quotes in there then you need to escape them by first quitting the main single-quote pair.



          That is needed also if you need to pass variables (eg hostname and/or port number) from your shell to the one-liner script. For instance:



          hostname=1.2.3.4
          portnumber=1234

          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/'"$hostname"'/'"$portnumber"' | grep "something"')


          Here I'm assuming that $hostname and $portnumber values can be trusted, ie provided by you or by other trusted sources that won't give illegitimate, invalid, or dangerous values.






          share|improve this answer















          telnet expects a tty on its stdin, but timeout takes that away.



          If you really insist on using telnet you may do so by adding --foreground option to timeout, as in:



          output=$(timeout --foreground --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something")


          Besides, if you can have nc on your system you should really rather use that for your purpose:



          output=$(timeout 3 nc 1.2.3.4 1234 | grep "something")


          If neither nc nor timeout --foreground are an option for you then you really need an alternative to telnet that won’t need a tty.



          I see you tagged your question bash, so you could use Bash’s own networking facilities, and thus your line might become like:



          output=$(timeout 3 cat < /dev/tcp/1.2.3.4/1234 | grep "something")


          If not even cat is an option then you could replace it with a one-liner script in Bash, like in:



          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/1.2.3.4/1234 | grep "something"')


          Hoping that at least stdbuf (which is part of standard coreutils package) is available in your system.



          In this last alternative however pay attention to your grep regex: if you have single-quotes in there then you need to escape them by first quitting the main single-quote pair.



          That is needed also if you need to pass variables (eg hostname and/or port number) from your shell to the one-liner script. For instance:



          hostname=1.2.3.4
          portnumber=1234

          output=$(timeout 3 stdbuf -oL bash -c 'while read line ; do echo "$line" ; done < /dev/tcp/'"$hostname"'/'"$portnumber"' | grep "something"')


          Here I'm assuming that $hostname and $portnumber values can be trusted, ie provided by you or by other trusted sources that won't give illegitimate, invalid, or dangerous values.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 11 hours ago

























          answered yesterday









          LL3LL3

          1565




          1565












          • Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

            – Ken Oh
            23 hours ago











          • @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

            – LL3
            16 hours ago











          • Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

            – Ken Oh
            13 hours ago






          • 1





            @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

            – LL3
            11 hours ago











          • I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

            – Ken Oh
            11 hours ago


















          • Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

            – Ken Oh
            23 hours ago











          • @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

            – LL3
            16 hours ago











          • Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

            – Ken Oh
            13 hours ago






          • 1





            @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

            – LL3
            11 hours ago











          • I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

            – Ken Oh
            11 hours ago

















          Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

          – Ken Oh
          23 hours ago





          Unfortunately, I don't have nc and my timeout also lacks --foreground (and anything other than -s or --signal).

          – Ken Oh
          23 hours ago













          @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

          – LL3
          16 hours ago





          @KenOh Having noted the bash tag in your question I’ve updated my answer for more alternatives , but if bash is actually not an option for you then please specify what system you are in

          – LL3
          16 hours ago













          Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

          – Ken Oh
          13 hours ago





          Sorry, I should have noted that it's on RHEL 6, with the complication that a lot of the utility commands being bare bones, and I'm not able to change any of that. I really appreciate the one-liner suggestions. They actually set the variable where the timeout versions do not, however they hang forever if the the link is down. My original thought was to send it to the background via &, but that failed when I couldn't pull variables from the background process to the current script. I may be barking up the wrong tree and should use a subscript with exit codes or something.

          – Ken Oh
          13 hours ago




          1




          1





          @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

          – LL3
          11 hours ago





          @KenOh Oh right, sorry, I had forgotten about that requirement! I’ve updated my answer again for the one-liner script, making it use timeout again in place of the -t 3 option of read which would timeout only after the connection was at least established. Anyway, besides the one-liner script, doesn’t RHEL6 even has cat ?!

          – LL3
          11 hours ago













          I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

          – Ken Oh
          11 hours ago






          I'm marking this as answered. Thank you very much! I do have to figure out why I'm having problems inserting variables into my telnet targets (e.g. /dev/tcp/$ip/$port seems to not pass the variables), but I suppose that's a different question.

          – Ken Oh
          11 hours ago














          1














          It is the standard error that is not being redirected (when your link is down)!
          This will solve your problem, because it redirects the standard error to the standard output.



          output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something") 





          share|improve this answer























          • I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

            – Ken Oh
            yesterday












          • Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

            – Luciano Andress Martini
            yesterday












          • Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

            – Ken Oh
            yesterday
















          1














          It is the standard error that is not being redirected (when your link is down)!
          This will solve your problem, because it redirects the standard error to the standard output.



          output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something") 





          share|improve this answer























          • I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

            – Ken Oh
            yesterday












          • Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

            – Luciano Andress Martini
            yesterday












          • Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

            – Ken Oh
            yesterday














          1












          1








          1







          It is the standard error that is not being redirected (when your link is down)!
          This will solve your problem, because it redirects the standard error to the standard output.



          output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something") 





          share|improve this answer













          It is the standard error that is not being redirected (when your link is down)!
          This will solve your problem, because it redirects the standard error to the standard output.



          output=$(timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something") 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Luciano Andress MartiniLuciano Andress Martini

          4,1241137




          4,1241137












          • I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

            – Ken Oh
            yesterday












          • Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

            – Luciano Andress Martini
            yesterday












          • Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

            – Ken Oh
            yesterday


















          • I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

            – Ken Oh
            yesterday












          • Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

            – Luciano Andress Martini
            yesterday












          • Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

            – Ken Oh
            yesterday

















          I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

          – Ken Oh
          yesterday






          I meant to say I've tried that. Puzzlingly, it doesn't change the behavior one bit (even if I output it to a file). To be clear, I don't need stderr. I actually do want what is being grepped.

          – Ken Oh
          yesterday














          Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

          – Luciano Andress Martini
          yesterday






          Can you post a example of what are being returned? I tested here, and it worked fine... Are you really running bash?

          – Luciano Andress Martini
          yesterday














          Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

          – Ken Oh
          yesterday






          Sure! timeout --signal=9 3 telnet 1.2.3.4 1234 2>&1 | grep "something" just returns something while timeout --signal=9 3 telnet 1.2.3.4 1234 | grep "something" returns something Connection Closed by foreign host.

          – Ken Oh
          yesterday


















          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%2f508806%2fsetting-variable-output-from-timeout%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







          -bash, telnet, timeout, variable

          Popular posts from this blog

          Frič See also Navigation menuinternal link

          Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

          fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox