Script to ping every hour and email failure

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











up vote
-1
down vote

favorite












I'm trying to write a shell script to ping 5 hosts I have once every 1 hour and if it receives any failure from any of those hosts, it sends an email alert with the results from this failing ping.










share|improve this question



























    up vote
    -1
    down vote

    favorite












    I'm trying to write a shell script to ping 5 hosts I have once every 1 hour and if it receives any failure from any of those hosts, it sends an email alert with the results from this failing ping.










    share|improve this question

























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I'm trying to write a shell script to ping 5 hosts I have once every 1 hour and if it receives any failure from any of those hosts, it sends an email alert with the results from this failing ping.










      share|improve this question















      I'm trying to write a shell script to ping 5 hosts I have once every 1 hour and if it receives any failure from any of those hosts, it sends an email alert with the results from this failing ping.







      shell scripting ping






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 19 '17 at 12:56









      Jeff Schaller

      33.3k850112




      33.3k850112










      asked Feb 17 '17 at 2:39









      Katkota

      414




      414




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Something like this should work:



           #!/bin/bash

          ping_targets="server1 server2 server3 server4 server5"
          failed_hosts=""

          for i in $ping_targets
          do
          ping -c 1 $i > /dev/null
          if [ $? -ne 0 ]; then
          if [ "$failed_hosts" == "" ]; then
          failed_hosts="$i"
          else
          failed_hosts="$failed_hosts, $i"
          fi
          fi
          done

          if [ "$failed_hosts" != "" ]; then
          echo $failed_hosts| mailx -s "Failed ping targets" email@domain
          fi


          Put it in a script, change the hostnames and email address, make it executable, and add an entry to cron so it runs once per hour.






          share|improve this answer




















          • Thank you very much.. I had to make some modification but your script worked great!!
            – Katkota
            Feb 17 '17 at 22:15










          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%2f345630%2fscript-to-ping-every-hour-and-email-failure%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          Something like this should work:



           #!/bin/bash

          ping_targets="server1 server2 server3 server4 server5"
          failed_hosts=""

          for i in $ping_targets
          do
          ping -c 1 $i > /dev/null
          if [ $? -ne 0 ]; then
          if [ "$failed_hosts" == "" ]; then
          failed_hosts="$i"
          else
          failed_hosts="$failed_hosts, $i"
          fi
          fi
          done

          if [ "$failed_hosts" != "" ]; then
          echo $failed_hosts| mailx -s "Failed ping targets" email@domain
          fi


          Put it in a script, change the hostnames and email address, make it executable, and add an entry to cron so it runs once per hour.






          share|improve this answer




















          • Thank you very much.. I had to make some modification but your script worked great!!
            – Katkota
            Feb 17 '17 at 22:15














          up vote
          6
          down vote



          accepted










          Something like this should work:



           #!/bin/bash

          ping_targets="server1 server2 server3 server4 server5"
          failed_hosts=""

          for i in $ping_targets
          do
          ping -c 1 $i > /dev/null
          if [ $? -ne 0 ]; then
          if [ "$failed_hosts" == "" ]; then
          failed_hosts="$i"
          else
          failed_hosts="$failed_hosts, $i"
          fi
          fi
          done

          if [ "$failed_hosts" != "" ]; then
          echo $failed_hosts| mailx -s "Failed ping targets" email@domain
          fi


          Put it in a script, change the hostnames and email address, make it executable, and add an entry to cron so it runs once per hour.






          share|improve this answer




















          • Thank you very much.. I had to make some modification but your script worked great!!
            – Katkota
            Feb 17 '17 at 22:15












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          Something like this should work:



           #!/bin/bash

          ping_targets="server1 server2 server3 server4 server5"
          failed_hosts=""

          for i in $ping_targets
          do
          ping -c 1 $i > /dev/null
          if [ $? -ne 0 ]; then
          if [ "$failed_hosts" == "" ]; then
          failed_hosts="$i"
          else
          failed_hosts="$failed_hosts, $i"
          fi
          fi
          done

          if [ "$failed_hosts" != "" ]; then
          echo $failed_hosts| mailx -s "Failed ping targets" email@domain
          fi


          Put it in a script, change the hostnames and email address, make it executable, and add an entry to cron so it runs once per hour.






          share|improve this answer












          Something like this should work:



           #!/bin/bash

          ping_targets="server1 server2 server3 server4 server5"
          failed_hosts=""

          for i in $ping_targets
          do
          ping -c 1 $i > /dev/null
          if [ $? -ne 0 ]; then
          if [ "$failed_hosts" == "" ]; then
          failed_hosts="$i"
          else
          failed_hosts="$failed_hosts, $i"
          fi
          fi
          done

          if [ "$failed_hosts" != "" ]; then
          echo $failed_hosts| mailx -s "Failed ping targets" email@domain
          fi


          Put it in a script, change the hostnames and email address, make it executable, and add an entry to cron so it runs once per hour.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 17 '17 at 4:32









          Warwick

          1,322715




          1,322715











          • Thank you very much.. I had to make some modification but your script worked great!!
            – Katkota
            Feb 17 '17 at 22:15
















          • Thank you very much.. I had to make some modification but your script worked great!!
            – Katkota
            Feb 17 '17 at 22:15















          Thank you very much.. I had to make some modification but your script worked great!!
          – Katkota
          Feb 17 '17 at 22:15




          Thank you very much.. I had to make some modification but your script worked great!!
          – Katkota
          Feb 17 '17 at 22:15

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f345630%2fscript-to-ping-every-hour-and-email-failure%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?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?