Mac What is wrong with this Bash script?

Rownzy

macrumors newbie
Original poster
Jun 17, 2015
5
0
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.
 
Last edited by a moderator:

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.
 
Comment

Rownzy

macrumors newbie
Original poster
Jun 17, 2015
5
0
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.
Thanks for your reply.
Any suggestions on how to make it work ?
 
Comment

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.
 
Comment

Rownzy

macrumors newbie
Original poster
Jun 17, 2015
5
0
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.
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.
 
Comment

dmi

macrumors regular
Dec 21, 2010
120
2
Code:
ping -c 1
 
Last edited by a moderator:
Comment

antonis

macrumors 68020
Jun 10, 2011
2,085
1,007
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
 
Last edited by a moderator:
Comment

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.
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.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.