iup

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

iup.sh (1487B)


      1 #!/bin/bash
      2 
      3 f="/run/user/$UID/probe_up"
      4 df="/run/user/$UID/probe_up_detail"
      5 rm -f $f
      6 
      7 up() {
      8 	#ip route | awk '{ if (/^default/) c=$5; if (c ~ /^tun/ ) { o="VPN"; c=0; } if ( c ~ /^wlp/ ) { o="CLEAR"; c=0; } } END { print o }'
      9 	#ip route | awk 'BEGIN { o="CLEAR";} { if ( /^0.0.0.0/ ) { if ($5 ~ /^tun/) { o="VPN"; c=""; } } } END { print o; }' >&3
     10 	truncate -s0 $df
     11 	exec 3<>$df
     12 	r=`ip route | awk 'BEGIN { o="CLEAR";} { if ( $2 ~ dev && $3 ~ /^tun/ ) o="VPN" } END { print o; }'`
     13        	echo -e "\033[32m$r\033[39m" >&3
     14 	exec 3>&-
     15 	#exit 0
     16 	if [ ! -f $f ]; then
     17 		logger -p info "iup up"
     18 	fi
     19 	touch $f
     20 }
     21 down() {
     22 	truncate -s0 $df
     23 	exec 3<>$df
     24 	echo "DOWN" >&3
     25 	exec 3>&-
     26 	#exit 0
     27 	if [ -f $f ]; then
     28 		logger -p info "iup down"
     29 	fi
     30 	rm -f $f
     31 }
     32 
     33 argh() {
     34 	logger -p info "iup die"
     35 	rm -f $f
     36 	exit 0
     37 }
     38 
     39 IUP_DELAY=${IUP_DELAY:-$1}
     40 if [ -z $IUP_DELAY ]; then
     41 	IUP_DELAY=5
     42 fi
     43 
     44 hosts=()
     45 while read h; do
     46 	hosts+=($h)
     47 done < $HOME/.config/iup 2> /dev/null
     48 if [ ${#hosts[@]} -eq "0" ]; then
     49 	# use cnn, google as default
     50 	hosts=(8.8.8.8 151.101.193.67)
     51 	logger -p warn missing hosts input file, using default: ${hosts[@]}
     52 fi
     53 
     54 logger -p debug iup started with delay $IUP_DELAY hosts ${hosts[@]}
     55 
     56 trap argh SIGINT
     57 trap argh SIGTERM
     58 trap argh SIGQUIT
     59 
     60 while true; do
     61 	isup=""
     62 	for h in ${hosts[@]}; do
     63 		ping -c1 $h -w1 -q &> /dev/null
     64 		if [ $? -eq '0' ]; then
     65 			up
     66 			isup=1
     67 			#logger -p debug "ping success $h"
     68 			break
     69 		fi
     70 	done
     71 	if [ -z $isup ]; then
     72 		down
     73 	fi
     74 	sleep $IUP_DELAY
     75 done