123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #!/bin/sh
- showHelp () {
- echo "Usage:"
- echo -e " nettest -h\t\t- this message"
- echo -e " nettest -g\t\t- sends IP"
- echo -e " nettest -fg\t\t- doesn't check connection, sends IP"
- echo -e " nettest -t\t\t- tests and prints vars"
- echo -e " nettest -c\t\t- checks pass/fail log"
- echo -e " nettest -cmc\t\t- for cron to check if last 'get' was missed"
- echo -e " nettest -ts\t\t- disables the send the IP for time"
- echo -e " nettest -ps\t\t- a shortcut for clearing the quere"
- }
- dateTest () {
- TIME_LIMIT=`cat /.ip_timeout`
- CURRENT_TIME=`date "+%s"`
- if [ $TIME_LIMIT ];then
- if [ $CURRENT_TIME -ge $TIME_LIMIT ];then
- CHECK3="pass"
- else
- CHECK3="fail"
- fi
- else
- CHECK3="pass"
- fi
- }
- networkTest () {
- #$LO0 is loop back, used for testing purposes
- #$EN0 is wired internet, $EN1 wireless internet
- #$FW0 is firewire internet, yeah, lmao...
- # LO0=`/sbin/ifconfig lo0|grep inet\ | cut -d\ -f 2`
- EN0=`/sbin/ifconfig en0|grep inet\ |cut -d\ -f 2`
- EN1=`/sbin/ifconfig en1|grep inet\ |cut -d\ -f 2`
- FW0=`/sbin/ifconfig fw0|grep inet\ | cut -d\ -f 2`
- #checks if connected to router
- if [ $EN0 $EN1 $FW0 ]; then
- CHECK1="pass"
- ping -o www.google.com &>/dev/null
- EXIT_STATUS=`echo $?`
- #checks if connected to interwebz
- if [ $EXIT_STATUS ]; then
- CHECK2="pass"
- else
- CHECK2="fail"
- fi
- else
- CHECK1="fail"
- CHECK2="fail"
- fi
- }
- sendEval () {
- # networkTest
- if [ $CHECK1 == "pass" ]; then
- if [ $CHECK2 == "pass" ]; then
- /usr/local/sbin/getip
- date "+%D %T PASSED">/.get_log 2>/dev/null
- else
- date "+%D %T CHECK2">/.get_log 2>/dev/null
- fi
- else
- date "+%D %T CHECK1">/.get_log 2>/dev/null
- fi
- echo $LO0 $EN0 $EN1 $FW0 $CHECK1 $CHECK2 $EXIT_STATUS>>/.stat_log
- }
- if [ $# = 0 ]; then
- networkTest
- if [ $CHECK2 == "pass" ]; then
- echo "You Havez Interwebz"
- else
- echo "not connected..."
- fi
- fi
- while [ $# -gt 0 ]; do
- ARG1=$1
- case $ARG1 in
- #sees if computer is connected; if so, sends ip
- "--get" | "-g" )
- dateTest
- if [ $CHECK3 == "pass" ]; then
- networkTest
- sendEval
- else
- date "+%D %T SKIPED">/.get_log 2>/dev/null
- fi
- break ;;
- #a unneeded way to force run it
- "--force" | "-fg" )
- /usr/local/sbin/getip
- break ;;
- #an added way to check the log files
- "--check" | "-c" )
- cat /.get_log
- break ;;
- #a way to check the vars
- "--test" | "-t" )
- networkTest
- echo $LO0 $EN0 $EN1 $FW0 $CHECK1 $CHECK2 $EXIT_STATUS
- break ;;
- #this is for checking if a run was missed
- "-cmc" )
- LAST_RUN=`cat /.get_log|cut -d\ -f 3`
- if [ $LAST_RUN ]; then
- if [ $LAST_RUN == "FAILED" ]; then
- networkTest
- sendEval
- else
- date "+%D %T NONEED">/.get_log 2>/dev/null
- fi
- else
- networkTest
- sendEval
- fi
- break ;;
- "-ts" )
- if [ $# -gt 1 ]; then
- if [ $# == 3 ];then
- ARG2=$2
- ARG3=$3
- if [ $ARG2 == "s" ]; then
- TIMESTAMP=$ARG3
- fi
- if [ $ARG2 == "m" ]; then
- TIMESTAMP=`echo $ARG3*60|bc`
- fi
- if [ $ARG2 == "h" ];then
- TIMESTAMP=`echo $ARG3*3600|bc`
- fi
- fi
- else
- TIMESTAMP=21600
- fi
- date "+%s+$TIMESTAMP"|bc>/.ip_timeout
- break ;;
- "-ps" ) sudo postsuper -d ALL
- break ;;
- "-cps" )
- # QUEUE_SIZE=`mailq|wc -l`
- # if [ $QUEUE_SIZE -ge 7 ];then
- /usr/sbin/postsuper -d ALL
- # fi
- break ;;
- "help" | * ) showHelp
- break ;;
- esac
- done
|