ping: show only results

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











up vote
1
down vote

favorite
1












Is it possible to only show the amount of milliseconds when pinging instead of the whole result page?



I want to check if my servers are online, so I want to return "OK xyz ms" or "FAIL". I am currently doing this like so:



#!/bin/sh
ergebnis=$(ping -qc1 google.com)
ok=$?
avg=$(echo -e "$ergebnis" | tail -n1 | awk 'print $4' | cut -f 2 -d "/")

if [ $ok -eq 0 ]
then
echo "OK $avg ms"
else
echo "FAIL"
fi


However, this uses quite a few pipes and since I am running this command pretty often to monitor my servers, I am wondering if there is a "smarter" approach. I am also afraid my pipes might not work properly when the ping command failes.







share|improve this question
























    up vote
    1
    down vote

    favorite
    1












    Is it possible to only show the amount of milliseconds when pinging instead of the whole result page?



    I want to check if my servers are online, so I want to return "OK xyz ms" or "FAIL". I am currently doing this like so:



    #!/bin/sh
    ergebnis=$(ping -qc1 google.com)
    ok=$?
    avg=$(echo -e "$ergebnis" | tail -n1 | awk 'print $4' | cut -f 2 -d "/")

    if [ $ok -eq 0 ]
    then
    echo "OK $avg ms"
    else
    echo "FAIL"
    fi


    However, this uses quite a few pipes and since I am running this command pretty often to monitor my servers, I am wondering if there is a "smarter" approach. I am also afraid my pipes might not work properly when the ping command failes.







    share|improve this question






















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      Is it possible to only show the amount of milliseconds when pinging instead of the whole result page?



      I want to check if my servers are online, so I want to return "OK xyz ms" or "FAIL". I am currently doing this like so:



      #!/bin/sh
      ergebnis=$(ping -qc1 google.com)
      ok=$?
      avg=$(echo -e "$ergebnis" | tail -n1 | awk 'print $4' | cut -f 2 -d "/")

      if [ $ok -eq 0 ]
      then
      echo "OK $avg ms"
      else
      echo "FAIL"
      fi


      However, this uses quite a few pipes and since I am running this command pretty often to monitor my servers, I am wondering if there is a "smarter" approach. I am also afraid my pipes might not work properly when the ping command failes.







      share|improve this question












      Is it possible to only show the amount of milliseconds when pinging instead of the whole result page?



      I want to check if my servers are online, so I want to return "OK xyz ms" or "FAIL". I am currently doing this like so:



      #!/bin/sh
      ergebnis=$(ping -qc1 google.com)
      ok=$?
      avg=$(echo -e "$ergebnis" | tail -n1 | awk 'print $4' | cut -f 2 -d "/")

      if [ $ok -eq 0 ]
      then
      echo "OK $avg ms"
      else
      echo "FAIL"
      fi


      However, this uses quite a few pipes and since I am running this command pretty often to monitor my servers, I am wondering if there is a "smarter" approach. I am also afraid my pipes might not work properly when the ping command failes.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 6 '17 at 12:54









      mfnalex

      1167




      1167




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Another awk variation:



          ping -qc1 google.com 2>&1 | awk -F'/' 'END print (/^rtt/? "OK "$5" ms":"FAIL") '



          • -F'/' - treat slash / as field separator

          Example output:



          OK 47.090 ms





          share|improve this answer


















          • 1




            Nice, relying on the last line being kept for END processing!
            – Stephen Kitt
            Dec 6 '17 at 13:38










          • @StephenKitt, thanks ...
            – RomanPerekhrest
            Dec 6 '17 at 14:05










          • Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
            – mfnalex
            Dec 8 '17 at 15:05

















          up vote
          3
          down vote













          There’s not much you can do with ping itself, but you can do all the processing in AWK, reducing the number of pipes, processes etc.:



          ping -qc1 google.com 2>&1 | awk -F/ '/^rtt/ printf "OK %.2f msn", $5; ok = 1 END if (!ok) print "FAIL" '





          share|improve this answer






















          • Thanks, that is definitely better than my solution.
            – mfnalex
            Dec 6 '17 at 13:34

















          up vote
          0
          down vote













          If you are not too concerned about the exact error message then how about



          ping google.com | grep -Po "time.*"





          share|improve this answer




















            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f409203%2fping-show-only-results%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Another awk variation:



            ping -qc1 google.com 2>&1 | awk -F'/' 'END print (/^rtt/? "OK "$5" ms":"FAIL") '



            • -F'/' - treat slash / as field separator

            Example output:



            OK 47.090 ms





            share|improve this answer


















            • 1




              Nice, relying on the last line being kept for END processing!
              – Stephen Kitt
              Dec 6 '17 at 13:38










            • @StephenKitt, thanks ...
              – RomanPerekhrest
              Dec 6 '17 at 14:05










            • Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
              – mfnalex
              Dec 8 '17 at 15:05














            up vote
            3
            down vote



            accepted










            Another awk variation:



            ping -qc1 google.com 2>&1 | awk -F'/' 'END print (/^rtt/? "OK "$5" ms":"FAIL") '



            • -F'/' - treat slash / as field separator

            Example output:



            OK 47.090 ms





            share|improve this answer


















            • 1




              Nice, relying on the last line being kept for END processing!
              – Stephen Kitt
              Dec 6 '17 at 13:38










            • @StephenKitt, thanks ...
              – RomanPerekhrest
              Dec 6 '17 at 14:05










            • Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
              – mfnalex
              Dec 8 '17 at 15:05












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Another awk variation:



            ping -qc1 google.com 2>&1 | awk -F'/' 'END print (/^rtt/? "OK "$5" ms":"FAIL") '



            • -F'/' - treat slash / as field separator

            Example output:



            OK 47.090 ms





            share|improve this answer














            Another awk variation:



            ping -qc1 google.com 2>&1 | awk -F'/' 'END print (/^rtt/? "OK "$5" ms":"FAIL") '



            • -F'/' - treat slash / as field separator

            Example output:



            OK 47.090 ms






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 6 '17 at 13:38









            Stephen Kitt

            143k22310374




            143k22310374










            answered Dec 6 '17 at 13:32









            RomanPerekhrest

            22.4k12145




            22.4k12145







            • 1




              Nice, relying on the last line being kept for END processing!
              – Stephen Kitt
              Dec 6 '17 at 13:38










            • @StephenKitt, thanks ...
              – RomanPerekhrest
              Dec 6 '17 at 14:05










            • Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
              – mfnalex
              Dec 8 '17 at 15:05












            • 1




              Nice, relying on the last line being kept for END processing!
              – Stephen Kitt
              Dec 6 '17 at 13:38










            • @StephenKitt, thanks ...
              – RomanPerekhrest
              Dec 6 '17 at 14:05










            • Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
              – mfnalex
              Dec 8 '17 at 15:05







            1




            1




            Nice, relying on the last line being kept for END processing!
            – Stephen Kitt
            Dec 6 '17 at 13:38




            Nice, relying on the last line being kept for END processing!
            – Stephen Kitt
            Dec 6 '17 at 13:38












            @StephenKitt, thanks ...
            – RomanPerekhrest
            Dec 6 '17 at 14:05




            @StephenKitt, thanks ...
            – RomanPerekhrest
            Dec 6 '17 at 14:05












            Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
            – mfnalex
            Dec 8 '17 at 15:05




            Thanks, seems to be the nicest solution. Although it was not faster than my way, it seems more elegant.
            – mfnalex
            Dec 8 '17 at 15:05












            up vote
            3
            down vote













            There’s not much you can do with ping itself, but you can do all the processing in AWK, reducing the number of pipes, processes etc.:



            ping -qc1 google.com 2>&1 | awk -F/ '/^rtt/ printf "OK %.2f msn", $5; ok = 1 END if (!ok) print "FAIL" '





            share|improve this answer






















            • Thanks, that is definitely better than my solution.
              – mfnalex
              Dec 6 '17 at 13:34














            up vote
            3
            down vote













            There’s not much you can do with ping itself, but you can do all the processing in AWK, reducing the number of pipes, processes etc.:



            ping -qc1 google.com 2>&1 | awk -F/ '/^rtt/ printf "OK %.2f msn", $5; ok = 1 END if (!ok) print "FAIL" '





            share|improve this answer






















            • Thanks, that is definitely better than my solution.
              – mfnalex
              Dec 6 '17 at 13:34












            up vote
            3
            down vote










            up vote
            3
            down vote









            There’s not much you can do with ping itself, but you can do all the processing in AWK, reducing the number of pipes, processes etc.:



            ping -qc1 google.com 2>&1 | awk -F/ '/^rtt/ printf "OK %.2f msn", $5; ok = 1 END if (!ok) print "FAIL" '





            share|improve this answer














            There’s not much you can do with ping itself, but you can do all the processing in AWK, reducing the number of pipes, processes etc.:



            ping -qc1 google.com 2>&1 | awk -F/ '/^rtt/ printf "OK %.2f msn", $5; ok = 1 END if (!ok) print "FAIL" '






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 6 '17 at 13:14

























            answered Dec 6 '17 at 13:03









            Stephen Kitt

            143k22310374




            143k22310374











            • Thanks, that is definitely better than my solution.
              – mfnalex
              Dec 6 '17 at 13:34
















            • Thanks, that is definitely better than my solution.
              – mfnalex
              Dec 6 '17 at 13:34















            Thanks, that is definitely better than my solution.
            – mfnalex
            Dec 6 '17 at 13:34




            Thanks, that is definitely better than my solution.
            – mfnalex
            Dec 6 '17 at 13:34










            up vote
            0
            down vote













            If you are not too concerned about the exact error message then how about



            ping google.com | grep -Po "time.*"





            share|improve this answer
























              up vote
              0
              down vote













              If you are not too concerned about the exact error message then how about



              ping google.com | grep -Po "time.*"





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                If you are not too concerned about the exact error message then how about



                ping google.com | grep -Po "time.*"





                share|improve this answer












                If you are not too concerned about the exact error message then how about



                ping google.com | grep -Po "time.*"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 6 '17 at 13:28









                bu5hman

                1,164214




                1,164214



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409203%2fping-show-only-results%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay