Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Need a ping script

  1. #1
    Join Date
    Dec 2011
    Beans
    18

    Need a ping script

    I need to write (or acquire) a script that continuously pings a site (google will do), and then execute a sound file if my internet connection drops out. The reason for this is that I have an application that needs to be constantly connected to the internet and if that connection drops out, which it does, I need to know immediately so that I can reset the connection. Is such a script difficult to write? Any insight would be greatly appreciated. Thanks in advance. Also, I apologize if this topic is misplaced here. If so, I'll gladly take my request elsewhere.

  2. #2
    Join Date
    Sep 2006
    Beans
    7,019
    Distro
    Lubuntu Development Release

    play

    You can use exit codes from programs like ping in conditional statements like "if".

    This uses "play" from Sound eXchange

    Code:
    #!/bin/sh
    while sleep 60;
    do
      if ! ping -c 1 -w 1 8.8.8.8 > /dev/null; then
        /usr/bin/play /usr/share/sounds/freedesktop/stereo/camera-shutter.oga > /dev/null 2>&1
      fi
    done
    Edit: you can choose a target closer to you than 8.8.8.8, such as your gateway, and still verify connectivity.
    Last edited by Lars Noodén; October 8th, 2013 at 03:06 PM.

  3. #3
    Join Date
    Dec 2011
    Beans
    18

    Re: Need a ping script

    Great, thank you. This can be input in the gnome scheduler? I'd prefer to use to gui as opposed to the cron command line if possible.

  4. #4
    Join Date
    Sep 2006
    Beans
    7,019
    Distro
    Lubuntu Development Release

    cron

    It can be run from cron. The GUI for cron is just that, a GUI. So it goes to the same place as the text interface. So you have a choice of how you access cron but the finished product is the same.

  5. #5
    Join Date
    Dec 2011
    Beans
    18

    Re: Need a ping script

    Thanks alot, I appreciate the help. So I can replace the 8.8.8.8 with any IP address that I'd like it to ping, correct? Also, I would simply replace /usr/bin/play /usr/share/sounds/freedesktop/stereo/camera-shutter.oga with the path to my sound file? Thanks again.

  6. #6
    Join Date
    Sep 2006
    Beans
    7,019
    Distro
    Lubuntu Development Release

    Re: Need a ping script

    Also be sure to test it, maybe unplug the network cable or something, to make sure it plays sounds on your system. I had to add "play" from the package "sox" on my system, but maybe it is there on yours. As far as the notification sound goes, look around for files ending in .oga and give them a try to find one that is insistent without being too annoying.

    As an afterthought, the while loop should be discarded if you are running from cron. Just use the "if" statement by itself.

  7. #7
    Join Date
    Sep 2006
    Beans
    7,019
    Distro
    Lubuntu Development Release

    Re: Need a ping script

    Quote Originally Posted by matt2422 View Post
    Thanks alot, I appreciate the help. So I can replace the 8.8.8.8 with any IP address that I'd like it to ping, correct? Also, I would simply replace /usr/bin/play /usr/share/sounds/freedesktop/stereo/camera-shutter.oga with the path to my sound file? Thanks again.

    Yes 8.8.8.8 can be replaced with any ip address. The closer to the machine, the better.

    The sound file can be anything that you can find on your system that "play" can use.

    Again, if you are running from cron, take out the while loop so you don't end up with dozens and dozens of the script running at the same time. If you have only one copy running, then the loop is needed. But if you are using cron to do the work, then the loop gets in the way.

  8. #8
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    8,710
    Distro
    Lubuntu 13.10 Saucy Salamander

    Re: Need a ping script

    Quote Originally Posted by Lars Noodén View Post
    The closer to the machine, the better.
    +1. People running a server somewhere might not be happy with receiving an infinite stream of pings.
    About problems due to upgrading
    Bringing old hardware back to life.
    Please visit Quick Links -> Unanswered Posts

  9. #9
    Join Date
    Dec 2011
    Beans
    18

    Re: Need a ping script

    Good point. I'll set it to ping my gateway

  10. #10
    Join Date
    Dec 2011
    Beans
    18

    Re: Need a ping script

    So here's what I've got. Doesn't seem to be working. I'm sure I must have done something wrong, anything blaringly obvious?

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •