#!/bin/sh set -eu # check if any interface is plugged in and configured # then do a ping test to see if we have connectivity include_ping_test=1 ping_test_host=gentoo.org infinite=false timeout=10 get_interfaces() { local ifname iftype for ifname in /sys/class/net/*; do [ -h "${ifname}" ] || continue read iftype < ${ifname}/type [ "${iftype}" = "1" ] && printf "%s " ${ifname##*/} done return 0 } check_interfaces() { local ifcount=0 local carriers=0 # plugged in local configured=0 for dev in $(get_interfaces); do : $((ifcount += 1)) read carrier < /sys/class/net/$dev/carrier 2>/dev/null || carrier="" [ "${carrier}" = 1 ] && : $((carriers += 1)) read operstate < /sys/class/net/$dev/operstate 2>/dev/null || operstate="" [ "${operstate}" = up ] && : $((configured += 1)) done #[ $configured -eq $ifcount ] && [ $carriers -ge 1 ] && return 0 [ $configured -ge 1 ] && [ $carriers -ge 1 ] && return 0 return 1 } check_online() { local timeout=${timeout:-120} while $infinite || [ $timeout -gt 0 ] ; do check_interfaces && break sleep 1 : $((timeout -= 1)) done #echo 'plugged in' if [ "${include_ping_test}" = 1 ] && [ -n "${ping_test_host}" ]; then local timeout=${timeout:-120} while $infinite || [ $timeout -gt 0 ] ; do ping -c 1 $ping_test_host >/dev/null 2>&1 [ $? -eq 0 ] && break sleep 1 : $((timeout -= 1)) done fi #echo 'connected' } check_online