My provider sometimes kicks me and assigns a new IP address.
I'd like to write a little script which displays a message when I fall offline. What is the most reliable way to check whether I'm offline with a (bash?) script?
Printable View
My provider sometimes kicks me and assigns a new IP address.
I'd like to write a little script which displays a message when I fall offline. What is the most reliable way to check whether I'm offline with a (bash?) script?
If online means "connected to the network", ping your default gateway. If it means connected to the Internet, ping an internet server. If it is successful, you are "online".
#!/bin/bash
if ! ping -c 1 -w 2 4.2.2.2 &>/dev/null ; then
echo "The internet appears to be down"
else
echo "The internet appears to be up"
fi
Save as ping.sh, chmod 775 and you are good to go.