Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A bash script to speak network status UNIX
I created a small but useful bash script that simply takes a look at the network connection. It then speaks out an information message via the built-in text-to-speech system if the network isn't available, or if the connection is re-established.

I use it to see if the AirPort connection is lost, or if the network cable is plugged out. I run it on startup as deamon...

[robg adds: This worked as described for me, though I only tested it as a live script, not a startup task. Remember to make the script executable first (chmod a+x scriptname).]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[13,675 views]  

A bash script to speak network status | 6 comments | Create New Account
Click here to return to the 'A bash script to speak network status' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A bash script to speak network status
Authored by: mozart11 on Jan 12, '06 08:05:51AM

I just get an error of "A unknown token can't go here."



[ Reply to This | # ]
A bash script to speak network status
Authored by: merlyn on Jan 12, '06 10:32:16PM
You can replace;

...
SOMECOMMAND
if test "${?}" = 0
...
with simply

...
if SOMECOMMAND
...


[ Reply to This | # ]
A bash script to speak network status
Authored by: dreness on Jan 17, '06 04:13:42PM

Running this script as is would result in constant pinging of www.apple.com. Apple might not apprecaite this happening from lots of machines, so it's probably best to change "www.apple.com" to the IP address of your default gateway, or perhaps a DNS server operated by your ISP.



[ Reply to This | # ]
A bash script to speak network status
Authored by: ChristophLauer on Feb 05, '06 08:46:28AM
Hi, i am the anonymous author of this script. Here an enhanced version:

#!/bin/sh

HOST=www.freenet.de

# first initiate the values
if /sbin/ping -s1 -t4 -o $HOST &> /dev/null
then
NET=true
NETOLD=true
say "the internet connection is available."
else
NET=false
NETOLD=false
say "no internet connection."
fi

# endless loop
while true
do
if /usr/bin/sudo /sbin/ping -i0.1 -s0 -t2 -o $HOST &> /dev/null
then
NET=true;
LOST_NET_COUNT=0;
else
if [ $LOST_NET_COUNT == 1 ]
then
NET=false;
else
let "LOST_NET_COUNT += 1";
fi
fi
#echo NEW:$NET OLD:$NETOLD LOSTCOUN:$LOST_NET_COUNT
if [ $NET != $NETOLD ]
then
if [ $NET = true ]
then
say "the internet connection is available again." &
else
say "lost the internet connection." &
fi
fi
NETOLD=$NET
sleep 2
done
Please change the $HOST variable to the server of you choice...

[ Reply to This | # ]
A bash script to speak network status
Authored by: ChristophLauer on Feb 05, '06 09:11:42AM
Hi again, here an script that starts the above script named "lookForNetwork.sh" on my machine if it is'nt running always. I start this script whith my cron minutely. Start crontab -e and add the following line */1 * * * * /Users/christoph/bin/AutoRunLookForNetwork.sh but dont forget to change the path... Here is the script.

#!/bin/sh
export PROCS=`/usr/bin/sudo -u christoph /bin/ps -axo command > /tmp/ps.out ; /usr/bin/grep lookForNetwork /tmp/ps.out | /usr/bin/wc -l`

#echo $PROCS

if [ $PROCS == 0 ]
then
# echo "lookForNetwork.sh dont run. Starting now..."
/usr/bin/sudo -u christoph /Users/christoph/bin/lookForNetwork.command &
#else
# echo "lookForNetwork-sh runns always..."
fi

exit 0;


[ Reply to This | # ]
A bash script to speak network status
Authored by: cbt on Feb 06, '06 12:24:42PM

Hello,

You can also check your route table for a default route:
if (netstat -rn | grep -q ^default) ; then
  echo "Default route exists (we're online)."
else
  echo "Default route doesn't exists (we're NOT online)."
fi

The route deamon should update the routing table when the connection is lost.

--
Cole



[ Reply to This | # ]