testwifi.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/bin/bash
  2. #########################################################################
  3. # Check WIFI and Reachability #
  4. # Created by PM-DoIT #
  5. # ver 0.7 #
  6. #########################################################################
  7. #------------------------------ VARIABLES ------------------------------#
  8. DAT=$(date +%y%m%d" "%T)
  9. LOG="/var/log/wifi.log"
  10. WWW=/var/www/wifi/
  11. test -d $WWW || mkdir $WWW
  12. #------------------------------ NIC-CHECK ------------------------------#
  13. interfaces(){
  14. NIC=$(find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n' | grep -v wlan | head -1)
  15. WIC=$(find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n' | grep wlan | head -1)
  16. RTR=$(ip route | grep ^default | awk '{print $3}')
  17. }
  18. #------------------------------ IP-CHECK -------------------------------#
  19. address(){
  20. echo "------------------------------------"
  21. if [ ! -z "$NIC" ]; then LIP=$(ip a | grep $NIC | grep "state UP"); fi
  22. if [ ! -z "$WIC" ]; then WIP=$(ip a | grep $WIC | grep "state UP"); fi
  23. if [ ! -z "$NIC" ];
  24. then
  25. echo -e "- \e[1;32mExist\e[0m \e[1;34mLAN\e[0m interface [\e[1;34m$NIC\e[0m]"
  26. if [ ! -z "$LIP" ];
  27. then
  28. echo -e "- \e[1;34mLAN\e[0m interface is [\e[1;32mUP\e[0m]"
  29. IPA=$(ifconfig $NIC | grep -w inet | awk '{print $2}')
  30. echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
  31. if [ ! -z "$WIC" ];
  32. then
  33. echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
  34. if [ ! -z "$WIP" ];
  35. then
  36. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
  37. echo -e "- \e[1;31mDisabling\e[0m [\e[1;36m$WIC\e[0m]"
  38. ifconfig $WIC down
  39. else
  40. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
  41. fi
  42. else
  43. echo -e "- \e[1;31mNOT\e[0m exist \e[1;36mWIFI\e[0m interface"
  44. fi
  45. exit 1
  46. else
  47. echo -e "- \e[1;34mLAN\e[0m interface is [\e[1;31mDOWN\e[0m]"
  48. echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
  49. if [ ! -z "$WIP" ];
  50. then
  51. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
  52. IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
  53. echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
  54. else
  55. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
  56. echo -e "- \e[1;32mEnabling\e[0m [\e[1;36m$WIC\e[0m]"
  57. ifconfig $WIC up
  58. sleep 2
  59. /usr/bin/systemctl restart NetworkManager
  60. sleep 4
  61. interfaces
  62. IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
  63. echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
  64. fi
  65. fi
  66. elif [ ! -z "$WIC" ];
  67. then
  68. echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
  69. if [ ! -z "$WIP" ];
  70. then
  71. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
  72. IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
  73. echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
  74. else
  75. echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
  76. echo -e "- \e[1;32mEnabling\e[0m [\e[1;36m$WIC\e[0m]"
  77. ifconfig $WIC up
  78. sleep 2
  79. /usr/bin/systemctl restart NetworkManager
  80. sleep 4
  81. interfaces
  82. IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
  83. echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
  84. fi
  85. else
  86. echo -e "- \e[1;31mNOT\e[0m exist ANY interface"
  87. exit 1
  88. fi
  89. }
  90. #------------------------------ SIGNAL ---------------------------------#
  91. signal(){
  92. if [ -z "$WIC" ]; then
  93. exit 1
  94. else
  95. iwconfig $WIC | grep -i --color quality | sed 's/=/ /g' | sed 's/\// /g' | awk '{print $4,$3}' > /tmp/wifi
  96. SID=$(iwconfig $WIC | grep ESSID | sed 's/:/ /g' | sed 's/"//g' | awk '{print $5}')
  97. TOT=$(cat /tmp/wifi | awk '{print $1}')
  98. ACT=$(cat /tmp/wifi | awk '{print $2}')
  99. PER=$((100*$ACT/$TOT))
  100. echo $PER > /tmp/wifi
  101. fi
  102. }
  103. #----------------------------- FIX-WIFI --------------------------------#
  104. fixwifi(){
  105. echo -e "- \e[1;31mRestarting\e[0m interface [\e[1;36m$WIC\e[0m]"
  106. ifconfig $WIC down
  107. sleep 1
  108. iwconfig $WIC power off
  109. ifconfig $WIC up
  110. sleep 2
  111. /usr/bin/systemctl restart NetworkManager
  112. sleep 3
  113. echo "$DAT restarted $WIC" >> $LOG
  114. }
  115. #------------------------------ LATENCY --------------------------------#
  116. latency(){
  117. ping -c 2 $RTR > /tmp/wifi.ping
  118. MS=$(cat /tmp/wifi.ping | grep icmp_seq | sed 's/=/ /g' | awk '{print $10}' | sort | tail -1)
  119. echo "$DAT ${PER}% ${MS}ms" >> $LOG
  120. if [ -z "$MS" ]; then fixwifi; fi
  121. }
  122. #------------------------------- REPORT --------------------------------#
  123. report(){
  124. echo "------------------------------------"
  125. echo -e "Reachability [\e[1;32m$RTR\e[0m] (\e[1;33m$MS\e[0m)ms"
  126. echo -e "Wifi Link Quality [\e[1;32m$SID\e[0m] (\e[1;33m$PER\e[0m)%"
  127. echo "------------------------------------"
  128. }
  129. #------------------------------ GRAPHING -------------------------------#
  130. graphing(){
  131. /usr/bin/rrdtool update /var/www/wifi/wifi.rrd N:$PER
  132. echo -e "- \e[1;32mGenerating\e[0m [RRD] graphs"
  133. RRDPATH="/var/www/wifi/"
  134. REDL="#1E90FF"
  135. REDD="#00008B"
  136. #-------------------------------- HOUR ---------------------------------#
  137. rrdtool graph $RRDPATH/hour.png --title="6h Period of WiFi Signal Quality" --start -6h \
  138. --height=240 \
  139. --width=640 \
  140. --lower-limit=0 \
  141. --vertical-label='quality' \
  142. --font TITLE:12: \
  143. --font AXIS:8: \
  144. --font LEGEND:10: \
  145. --font UNIT:8: \
  146. DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
  147. LINE:wifi$REDD:"%" \
  148. AREA:wifi$REDL \
  149. GPRINT:wifi:LAST:"Current\:%8.2lf %s" \
  150. GPRINT:wifi:MIN:"Minimum\:%8.2lf %s" \
  151. GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
  152. #-------------------------------- DAY ----------------------------------#
  153. rrdtool graph $RRDPATH/day.png --title="Day Period of WiFi Signal Quality" --start -1d \
  154. --height=240 \
  155. --width=640 \
  156. --lower-limit=0 \
  157. --vertical-label='quality' \
  158. --font TITLE:12: \
  159. --font AXIS:8: \
  160. --font LEGEND:10: \
  161. --font UNIT:8: \
  162. DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
  163. LINE:wifi$REDD:"%" \
  164. AREA:wifi$REDL \
  165. GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s" \
  166. GPRINT:wifi:MIN:"Minimum\:%8.2lf %s" \
  167. GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
  168. #-------------------------------- WEEK ---------------------------------#
  169. rrdtool graph $RRDPATH/week.png --title="Week Period of WiFi Signal Quality" --start -1w \
  170. --height=240 \
  171. --width=640 \
  172. --lower-limit=0 \
  173. --vertical-label='quality' \
  174. --font TITLE:12: \
  175. --font AXIS:8: \
  176. --font LEGEND:10: \
  177. --font UNIT:8: \
  178. DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
  179. LINE:wifi$REDD:"%" \
  180. GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s" \
  181. GPRINT:wifi:MIN:"Minimum\:%8.2lf %s" \
  182. GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
  183. #------------------------------- MONTH ---------------------------------#
  184. rrdtool graph $RRDPATH/month.png --title="Month Period of WiFi Signal Quality" --start -1m \
  185. --height=240 \
  186. --width=640 \
  187. --lower-limit=0 \
  188. --vertical-label='quality' \
  189. --font TITLE:12: \
  190. --font AXIS:8: \
  191. --font LEGEND:10: \
  192. --font UNIT:8: \
  193. DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
  194. LINE:wifi$REDD:"%" \
  195. GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s" \
  196. GPRINT:wifi:MIN:"Minimum\:%8.2lf %s" \
  197. GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
  198. #-------------------------------- YEAR ---------------------------------#
  199. rrdtool graph $RRDPATH/year.png --title="Year Period of WiFi Signal Quality" --start -1y \
  200. --height=240 \
  201. --width=640 \
  202. --lower-limit=0 \
  203. --vertical-label='quality' \
  204. --font TITLE:12: \
  205. --font AXIS:8: \
  206. --font LEGEND:10: \
  207. --font UNIT:8: \
  208. DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
  209. LINE:wifi$REDD:"%" \
  210. GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s" \
  211. GPRINT:wifi:MIN:"Minimum\:%8.2lf %s" \
  212. GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
  213. }
  214. #-------------------------------- CORE ---------------------------------#
  215. clear
  216. interfaces
  217. address
  218. signal
  219. latency
  220. graphing
  221. report