iup

Ping script to record internet connection state
git clone git://git.defalsify.org/iup.git
Log | Files | Refs | LICENSE

commit 4b2c422417c480b55ea98c4303f82e2947fd90b1
Author: lash <dev@holbrook.no>
Date:   Fri, 14 Jan 2022 08:18:14 +0000

Initial commit

Diffstat:
ACHANGELOG | 4++++
ALICENSE | 14++++++++++++++
AVERSION | 1+
Aiup.service | 10++++++++++
Aiup.sh | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -0,0 +1,4 @@ +* 0.1.0 + - optional host list from config file + - customizable delay between ping rounds + - three types of detail; VPN, CLEAR or DOWN diff --git a/LICENSE b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/VERSION b/VERSION @@ -0,0 +1 @@ +0.1.0 diff --git a/iup.service b/iup.service @@ -0,0 +1,10 @@ +[Unit] +Description=Internet connection poller +After=multi-user.target + +[Service] +ExecStart=/usr/local/bin/iup.sh +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/iup.sh b/iup.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +f="/run/user/$UID/probe_up" +df="/run/user/$UID/probe_up_detail" +rm -f $f + +up() { + #ip route | awk '{ if (/^default/) c=$5; if (c ~ /^tun/ ) { o="VPN"; c=0; } if ( c ~ /^wlp/ ) { o="CLEAR"; c=0; } } END { print o }' + #ip route | awk 'BEGIN { o="CLEAR";} { if ( /^0.0.0.0/ ) { if ($5 ~ /^tun/) { o="VPN"; c=""; } } } END { print o; }' >&3 + truncate -s0 $df + exec 3<>$df + r=`ip route | awk 'BEGIN { o="CLEAR";} { if ( $2 ~ dev && $3 ~ /^tun/ ) o="VPN" } END { print o; }'` + echo -e "\033[32m$r\033[39m" >&3 + exec 3>&- + #exit 0 + if [ ! -f $f ]; then + logger -p info "iup up" + fi + touch $f +} +down() { + truncate -s0 $df + exec 3<>$df + echo "DOWN" >&3 + exec 3>&- + #exit 0 + if [ -f $f ]; then + logger -p info "iup down" + fi + rm -f $f +} + +argh() { + logger -p info "iup die" + rm -f $f + exit 0 +} + +IUP_DELAY=${IUP_DELAY:-$1} +if [ -z $IUP_DELAY ]; then + IUP_DELAY=5 +fi + +hosts=() +while read h; do + hosts+=($h) +done < $HOME/.config/iup 2> /dev/null +if [ ${#hosts[@]} -eq "0" ]; then + # use cnn, google as default + hosts=(8.8.8.8 151.101.193.67) + logger -p warn missing hosts input file, using default: ${hosts[@]} +fi + +logger -p debug iup started with delay $IUP_DELAY hosts ${hosts[@]} + +trap argh SIGINT +trap argh SIGTERM +trap argh SIGQUIT + +while true; do + isup="" + for h in ${hosts[@]}; do + ping -c1 $h -w1 -q &> /dev/null + if [ $? -eq '0' ]; then + up + isup=1 + #logger -p debug "ping success $h" + break + fi + done + if [ -z $isup ]; then + down + fi + sleep $IUP_DELAY +done