Bash/sh: How to detect that there is no internet connection anymore?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If your objective s to test networking in the kernel then best to start by specifying what you want to test. ping is OK to test IP connectivity (it's "pass" if a reply comes and "not known" if it doesn't) but I guess there's more to test than that in networking in the kernel.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I use this Bash script to log and reset my VPN connection. It can be easliy adapted, if you set OPEN_VPN_CMD to the script you want to have executed when the network is gone. I call this script from cron every 2 minutes. If you set debug_out to /dev/null, relevant messages are appended to /var/log/messages.
Code:
! /bin/sh
debug_out=/dev/null
#debug_out=/dev/stdout
NEXTHOP=star_pc.mydomain.com
OPEN_VPN_CMD="/etc/init.d/openvpn restart"
PING=/bin/ping
logger_opts="-t $0"
if [ "$debug_out" = "/dev/stdout" ]
then
logger_opts="$logger_opts -s"
fi
pckts_rcvd=`$PING -c 8 -q -W 2 $NEXTHOP | grep transm | awk '{print $4}'`
echo "host: $NEXTHOP, pckts_rcvd: $pckts_rcvd" >$debug_out
if [ $pckts_rcvd -eq 0 ]
then
echo "Connection with $NEXTHOP lost, resetting" | logger $logopts
$OPEN_VPN_CMD > $debug_out
else
echo "Connection with $NEXTHOP up, no action" | logger $logopts
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.