nettest 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/sh
  2. showHelp () {
  3. echo "Usage:"
  4. echo -e " nettest -h\t\t- this message"
  5. echo -e " nettest -g\t\t- sends IP"
  6. echo -e " nettest -fg\t\t- doesn't check connection, sends IP"
  7. echo -e " nettest -t\t\t- tests and prints vars"
  8. echo -e " nettest -c\t\t- checks pass/fail log"
  9. echo -e " nettest -cmc\t\t- for cron to check if last 'get' was missed"
  10. echo -e " nettest -ts\t\t- disables the send the IP for time"
  11. echo -e " nettest -ps\t\t- a shortcut for clearing the quere"
  12. }
  13. dateTest () {
  14. TIME_LIMIT=`cat /.ip_timeout`
  15. CURRENT_TIME=`date "+%s"`
  16. if [ $TIME_LIMIT ];then
  17. if [ $CURRENT_TIME -ge $TIME_LIMIT ];then
  18. CHECK3="pass"
  19. else
  20. CHECK3="fail"
  21. fi
  22. else
  23. CHECK3="pass"
  24. fi
  25. }
  26. networkTest () {
  27. #$LO0 is loop back, used for testing purposes
  28. #$EN0 is wired internet, $EN1 wireless internet
  29. #$FW0 is firewire internet, yeah, lmao...
  30. # LO0=`/sbin/ifconfig lo0|grep inet\ | cut -d\ -f 2`
  31. EN0=`/sbin/ifconfig en0|grep inet\ |cut -d\ -f 2`
  32. EN1=`/sbin/ifconfig en1|grep inet\ |cut -d\ -f 2`
  33. FW0=`/sbin/ifconfig fw0|grep inet\ | cut -d\ -f 2`
  34. #checks if connected to router
  35. if [ $EN0 $EN1 $FW0 ]; then
  36. CHECK1="pass"
  37. ping -o www.google.com &>/dev/null
  38. EXIT_STATUS=`echo $?`
  39. #checks if connected to interwebz
  40. if [ $EXIT_STATUS ]; then
  41. CHECK2="pass"
  42. else
  43. CHECK2="fail"
  44. fi
  45. else
  46. CHECK1="fail"
  47. CHECK2="fail"
  48. fi
  49. }
  50. sendEval () {
  51. # networkTest
  52. if [ $CHECK1 == "pass" ]; then
  53. if [ $CHECK2 == "pass" ]; then
  54. /usr/local/sbin/getip
  55. date "+%D %T PASSED">/.get_log 2>/dev/null
  56. else
  57. date "+%D %T CHECK2">/.get_log 2>/dev/null
  58. fi
  59. else
  60. date "+%D %T CHECK1">/.get_log 2>/dev/null
  61. fi
  62. echo $LO0 $EN0 $EN1 $FW0 $CHECK1 $CHECK2 $EXIT_STATUS>>/.stat_log
  63. }
  64. if [ $# = 0 ]; then
  65. networkTest
  66. if [ $CHECK2 == "pass" ]; then
  67. echo "You Havez Interwebz"
  68. else
  69. echo "not connected..."
  70. fi
  71. fi
  72. while [ $# -gt 0 ]; do
  73. ARG1=$1
  74. case $ARG1 in
  75. #sees if computer is connected; if so, sends ip
  76. "--get" | "-g" )
  77. dateTest
  78. if [ $CHECK3 == "pass" ]; then
  79. networkTest
  80. sendEval
  81. else
  82. date "+%D %T SKIPED">/.get_log 2>/dev/null
  83. fi
  84. break ;;
  85. #a unneeded way to force run it
  86. "--force" | "-fg" )
  87. /usr/local/sbin/getip
  88. break ;;
  89. #an added way to check the log files
  90. "--check" | "-c" )
  91. cat /.get_log
  92. break ;;
  93. #a way to check the vars
  94. "--test" | "-t" )
  95. networkTest
  96. echo $LO0 $EN0 $EN1 $FW0 $CHECK1 $CHECK2 $EXIT_STATUS
  97. break ;;
  98. #this is for checking if a run was missed
  99. "-cmc" )
  100. LAST_RUN=`cat /.get_log|cut -d\ -f 3`
  101. if [ $LAST_RUN ]; then
  102. if [ $LAST_RUN == "FAILED" ]; then
  103. networkTest
  104. sendEval
  105. else
  106. date "+%D %T NONEED">/.get_log 2>/dev/null
  107. fi
  108. else
  109. networkTest
  110. sendEval
  111. fi
  112. break ;;
  113. "-ts" )
  114. if [ $# -gt 1 ]; then
  115. if [ $# == 3 ];then
  116. ARG2=$2
  117. ARG3=$3
  118. if [ $ARG2 == "s" ]; then
  119. TIMESTAMP=$ARG3
  120. fi
  121. if [ $ARG2 == "m" ]; then
  122. TIMESTAMP=`echo $ARG3*60|bc`
  123. fi
  124. if [ $ARG2 == "h" ];then
  125. TIMESTAMP=`echo $ARG3*3600|bc`
  126. fi
  127. fi
  128. else
  129. TIMESTAMP=21600
  130. fi
  131. date "+%s+$TIMESTAMP"|bc>/.ip_timeout
  132. break ;;
  133. "-ps" ) sudo postsuper -d ALL
  134. break ;;
  135. "-cps" )
  136. # QUEUE_SIZE=`mailq|wc -l`
  137. # if [ $QUEUE_SIZE -ge 7 ];then
  138. /usr/sbin/postsuper -d ALL
  139. # fi
  140. break ;;
  141. "help" | * ) showHelp
  142. break ;;
  143. esac
  144. done