#!/bin/bash # check_inet. This script is distributed under GPLv3 license. # Author: bircoph MAXWAIT=30
if ( ifconfig ppp0 2>&1 | grep \"error fetching interface information\" ); then #check for stale connection if ps h -C pptp; then killall pptp; fi if ps h -C pppd; then killall pppd; fi #wait for finalizing i=0; while ps h -C pptp -C pppd; do sleep 1; i=$(( ++i )); if (( $i>$MAXWAIT )); then killall -9 pptp pppd; sleep 30; fi; done #resurrection rc-config restart net.ppp0; fi
### run cortel uplink check ### flag=\"/var/run/cortel.down\" ssh_cmd=\"ssh -i /home/andrew/.ssh/id_rsa_light bircoph@altair\" # ping pool pool=( ya.ru mail.ru google.ru kernel.org bbc.com yahoo.com opennet.ru ntp.ru time.nist.gov gnu.org corbina.net sf.net ) pool_len=${#pool[*]} # timing ping_hosts=3
# stateless cortel.down action cortel_down() { date -R > $flag $ssh_cmd /home/bircoph/cortel.down }
# ping the internet # return 0 on success, 1 on failure check_ping() { # counter for ping pool lenght ping_count=0
# select hosts from a pool for (( i=0; i < $ping_hosts; i++ )); do # pick up a random one index=$(( RANDOM % pool_len)) for (( j=0; j < $ping_count; j++ )); do [ $index -eq ${ping_pool[j]} ] && { index=$(( RANDOM % pool_len )) j=-1 } done # add ping index to pool ping_pool[ping_count]=$index ping_count=$((++ping_count))
# exit if everything is ok ( ping -I ppp0 -n -q -w 30 -c 2 ${pool[index]} ) && \\ return 0 done # we can be here only if all pings failed return 1 }
# check for remnant lock file (last mod time < bootup time) [ -f $flag ] && \\ [[ $(( `stat -c %Y $flag` )) -lt $(( `date +%s` - `gawk -F . \'{ print $1 }\' /proc/uptime` )) ]] && \\ rm -f $flag
# if pptp server is dead, assume down status ( ifconfig ppp0 2>&1 | grep \"error fetching interface information\" ) && \\ [ ! -f $flag ] && cortel_down
if [ -f $flag ]; then ( check_ping ) && { rm -f $flag $ssh_cmd /home/bircoph/cortel.up } else ( ! check_ping ) && cortel_down fi
|