Created
March 10, 2025 14:58
-
-
Save sjlongland/5c421ebfcbcd165e2530025987417f57 to your computer and use it in GitHub Desktop.
Internet status check for PPPoE on OpenBSD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
INTERFACE=pppoe0 | |
CACHEDIR=/var/cache/inetstatus | |
LASTSTATE=${CACHEDIR}/last | |
date=$( TZ=UTC date "+%Y-%m-%dT%H:%M:%SZ" ) | |
eval $( | |
ifconfig ${INTERFACE} \ | |
| sed -ne '/sid:/ { s/^.*PADI retries: \([0-9]*\) PADR retries: \([0-9]*\) *.*$/padi=\1 padr=\2/; p; }; /status:/ { s/^.*: \(.*\) *$/status="\1"/; p; }' | |
) | |
if [ -f "${LASTSTATE}" ]; then | |
eval "$(< ${LASTSTATE} )" | |
else | |
last_status=unknown | |
last_date=1970-01-01T00:00:00Z | |
fi | |
if [ "${status}" != "${last_status}" ] || [ "${VERBOSE}" = 1 ]; then | |
logger -t inetstatus daemon.notice "${INTERFACE} changed status ${last_status} (at ${last_date}) to ${status} (at ${date} PADI=${padi} PADR=${padr})" | |
echo "${last_date}>${date} PADI=${padi} PADR=${padr} STATUS=\"${last_status}\">\"${status}\"" | |
echo "last_status=\"${status}\" last_date=\"${date}\"" > "${LASTSTATE}" | |
elif [ "${status}" = "no carrier" ]; then | |
changed_ts=$( stat -f %m ${LASTSTATE} ) | |
now_ts=$( date +%s ) | |
delta=$(( ${now_ts} - ${changed_ts} )) | |
if [ ${delta} -gt 3600 ]; then | |
logger -t inetstatus daemon.notice "Rebooting due to prolonged outage" | |
touch ${LASTSTATE} | |
/usr/bin/uptime | |
/sbin/reboot | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment