How do you check in a script for internet connectivity?

Kind of: If you have internet do your thing, else exit here and do nothing.

(My solution was a ping, but then I get `ping: sendmsg: No route to host` in my cronjobs.)

Okay. It seems to work with ping and `if [ $? -eq 0 ]`. I just did the wrong check.

@natedalliard I was using a dig command to measure internet connectivity and if so, also get the latency


dig @ +timeout=1 > /tmp/latencecheck

if [ $? -eq 0 ]
time=$(awk '/Query time/{
if($4 < 60) { print "_";}
if($4 >= 60 && $4 <= 150) { print "-"; }
if($4 > 150) { print "¯"; }
}' /tmp/latencecheck)
echo $time | tee /tmp/latenceresult
echo "N" | tee /tmp/latenceresult

@solene Thx a lot, I will try that!

(Btw. you are my personal OpenBSD hero — hmm, is there a female form of hero?! — here on Mastodon)


nc -zw1 "$DOMAIN" $PORT > /dev/null 2>&1 && exit 0 || exit 1

Sign in to participate in the conversation - because anarchy is much more fun with friends. is a small Mastodon instance for and by the Chaos community surrounding the Chaos Computer Club. We provide a small community space - Be excellent to each other, and have a look at what that means around here.
Follow @ordnung for low-traffic instance-related updates.
The primary instance languages are German and English.