What is wrong with this Bash script?

Discussion in 'Mac Programming' started by Rownzy, Jun 17, 2015.

  1. Rownzy, Jun 17, 2015
    Last edited by a moderator: Jun 17, 2015

    Rownzy macrumors newbie

    Joined:
    Jun 17, 2015
    #1
    Hi,
    I`m trying to monitor a connection lost which happened sporadically.
    I tried to use the following script:

    Code:
    #!/bin/bash
    ping $HOST  8.8.8.8
    if [ $? -ne 0 ]; then
    date > path/tmp/logfile.text
    fi
    The ping is working, but when i timeout the log is not created...
    any ideas what I`m doing wrong?

    Thanks a lot.
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    Ping runs forever without additional arguments, so you will never reach your if clause. Secondly, the standard ping utility only takes one host as argument. Trying the above here immediately ends with a usage message.
     
  3. Rownzy thread starter macrumors newbie

    Joined:
    Jun 17, 2015
    #3
    Thanks for your reply.
    Any suggestions on how to make it work ?
     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    It depends on what you are trying to do, you can limit the pings to a specified amount with the -c flag, and also break up the ping line in two, first trying the local interface, then the remote host in a separate line.
     
  5. Rownzy thread starter macrumors newbie

    Joined:
    Jun 17, 2015
    #5
    I`m trying to monitor connection lost, i want to ping 8.8.8.8 and when the internet is down (Getting Timeout) it will log the time and date of when it occurred.
    I want to ping only 1 address.
     
  6. dmi, Jun 17, 2015
    Last edited by a moderator: Jun 17, 2015

    dmi macrumors regular

    Joined:
    Dec 21, 2010
  7. antonis, Jun 17, 2015
    Last edited by a moderator: Jun 17, 2015

    antonis macrumors 68000

    antonis

    Joined:
    Jun 10, 2011
    #7
    You can use netcat (nc) to check for a connection to a host (and port). In your case this would be:

    Code:
    nc -z -w2 some_host some_port
    The "if" part can remain as it is.

    -z will make sure that no data will be sent to the port you are checking
    -w2 will wait for 2 seconds for the reply
     
  8. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    But in what manner are you trying to monitor this? I mean if you are using the computer, it's obvious when the Internet is gone, if you are not then why does it matter? Doing this continuously will generate 3600 pings an hour to Google, perhaps they'll block you..

    In any case, you can set up a function that does what you want, but keep in mind that if internet is down you will flood your log with identical messages, but as your logging is set up now, only the last entry will be saved, i.e each entry overwrites the previous one. I would also not pick -c 1, because it's not uncommon for some packets to get lost even when everything is working as expected, so you will get false positives.

    Code:
    #!/bin/bash
    
    function ping_dns {
      ping -q -c 3 8.8.8.8
      if [ $? -ne 0 ]
      then
         echo $(date): Connection is down > path/tmp/logfile.text
      fi
    }
    
    while true
    do
      ping_dns
      sleep 60
    done
    
    This script sends 3 pings, then sleeps one minute and repeates forever.
     

Share This Page