Ver Fonte

Script for check and measure WiFi connection and update interface NICs

pmacko há 1 mês atrás
pai
commit
a592b96e25
1 ficheiros alterados com 231 adições e 0 exclusões
  1. 231 0
      testwifi.sh

+ 231 - 0
testwifi.sh

@@ -0,0 +1,231 @@
+#!/bin/bash
+#########################################################################
+#                    Check WIFI and Reachability                        #
+#                         Created by PM-DoIT                            #
+#                               ver 0.7                                 #
+#########################################################################
+
+#------------------------------ VARIABLES ------------------------------#
+DAT=$(date +%y%m%d" "%T)
+LOG="/var/log/wifi.log"
+WWW=/var/www/wifi/
+test -d $WWW || mkdir $WWW
+
+#------------------------------ NIC-CHECK ------------------------------#
+interfaces(){
+NIC=$(find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n' | grep -v wlan | head -1)
+WIC=$(find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n' | grep wlan | head -1)
+RTR=$(ip route | grep ^default | awk '{print $3}')
+}
+
+#------------------------------ IP-CHECK -------------------------------#
+address(){
+echo "------------------------------------"
+if [ ! -z "$NIC" ]; then LIP=$(ip a | grep $NIC | grep "state UP"); fi
+if [ ! -z "$WIC" ]; then WIP=$(ip a | grep $WIC | grep "state UP"); fi
+
+if [ ! -z "$NIC" ];
+then
+        echo -e "- \e[1;32mExist\e[0m \e[1;34mLAN\e[0m interface [\e[1;34m$NIC\e[0m]"
+        if [ ! -z "$LIP" ];
+        then
+                echo -e "- \e[1;34mLAN\e[0m interface is [\e[1;32mUP\e[0m]"
+                IPA=$(ifconfig $NIC | grep -w inet | awk '{print $2}')
+                echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
+                if [ ! -z "$WIC" ];
+                then
+                        echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
+                        if [ ! -z "$WIP" ];
+                        then
+                                echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
+                                echo -e "- \e[1;31mDisabling\e[0m [\e[1;36m$WIC\e[0m]"
+                                ifconfig $WIC down
+                        else
+                                echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
+                        fi
+                else
+                        echo -e "- \e[1;31mNOT\e[0m exist \e[1;36mWIFI\e[0m interface"
+                fi
+                exit 1
+        else
+                echo -e "- \e[1;34mLAN\e[0m interface is [\e[1;31mDOWN\e[0m]"
+                echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
+                if [ ! -z "$WIP" ];
+                then
+                        echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
+                        IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
+                        echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
+                else
+                        echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
+                        echo -e "- \e[1;32mEnabling\e[0m [\e[1;36m$WIC\e[0m]"
+                        ifconfig $WIC up
+                        sleep 2
+                        /usr/bin/systemctl restart NetworkManager
+                        sleep 4
+                        interfaces
+                        IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
+                        echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
+                fi
+        fi
+elif [ ! -z "$WIC" ];
+then
+        echo -e "- \e[1;32mExist\e[0m \e[1;36mWIFI\e[0m interface [\e[1;36m$WIC\e[0m]"
+        if [ ! -z "$WIP" ];
+        then
+                echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;32mUP\e[0m]"
+                IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
+                echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
+        else
+                echo -e "- \e[1;36mWIFI\e[0m interface is [\e[1;31mDOWN\e[0m]"
+                echo -e "- \e[1;32mEnabling\e[0m [\e[1;36m$WIC\e[0m]"
+                ifconfig $WIC up
+                sleep 2
+                /usr/bin/systemctl restart NetworkManager
+                sleep 4
+                interfaces
+                IPA=$(ifconfig $WIC | grep -w inet | awk '{print $2}')
+                echo -e "- \e[1;32mActual\e[0m IP address [\e[1;35m$IPA\e[0m]"
+        fi
+else
+        echo -e "- \e[1;31mNOT\e[0m exist ANY interface"
+        exit 1
+fi
+}
+
+#------------------------------ SIGNAL ---------------------------------#
+signal(){
+if [ -z "$WIC" ]; then
+exit 1
+else
+iwconfig $WIC | grep -i --color quality | sed 's/=/ /g' | sed 's/\// /g' | awk '{print $4,$3}' > /tmp/wifi
+SID=$(iwconfig $WIC | grep ESSID | sed 's/:/ /g' | sed 's/"//g' | awk '{print $5}')
+TOT=$(cat /tmp/wifi | awk '{print $1}')
+ACT=$(cat /tmp/wifi | awk '{print $2}')
+PER=$((100*$ACT/$TOT))
+echo $PER > /tmp/wifi
+fi
+}
+
+#----------------------------- FIX-WIFI --------------------------------#
+fixwifi(){
+echo -e "- \e[1;31mRestarting\e[0m interface [\e[1;36m$WIC\e[0m]"
+ifconfig $WIC down
+sleep 1
+iwconfig $WIC power off
+ifconfig $WIC up
+sleep 2
+/usr/bin/systemctl restart NetworkManager
+sleep 3
+echo "$DAT restarted $WIC" >> $LOG
+}
+
+#------------------------------ LATENCY --------------------------------#
+latency(){
+ping -c 2 $RTR > /tmp/wifi.ping
+MS=$(cat /tmp/wifi.ping | grep icmp_seq | sed 's/=/ /g' | awk '{print $10}' | sort | tail -1)
+echo "$DAT ${PER}% ${MS}ms" >> $LOG
+if [ -z "$MS" ]; then fixwifi; fi
+}
+
+#------------------------------- REPORT --------------------------------#
+report(){
+echo "------------------------------------"
+echo -e "Reachability [\e[1;32m$RTR\e[0m] (\e[1;33m$MS\e[0m)ms"
+echo -e "Wifi Link Quality [\e[1;32m$SID\e[0m] (\e[1;33m$PER\e[0m)%"
+echo "------------------------------------"
+}
+
+#------------------------------ GRAPHING -------------------------------#
+graphing(){
+/usr/bin/rrdtool update /var/www/wifi/wifi.rrd N:$PER
+echo -e "- \e[1;32mGenerating\e[0m [RRD] graphs"
+RRDPATH="/var/www/wifi/"
+REDL="#1E90FF"
+REDD="#00008B"
+#-------------------------------- HOUR ---------------------------------#
+rrdtool graph $RRDPATH/hour.png --title="6h Period of WiFi Signal Quality" --start -6h \
+--height=240 \
+--width=640 \
+--lower-limit=0 \
+--vertical-label='quality' \
+--font TITLE:12: \
+--font AXIS:8: \
+--font LEGEND:10: \
+--font UNIT:8: \
+DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
+LINE:wifi$REDD:"%" \
+AREA:wifi$REDL \
+GPRINT:wifi:LAST:"Current\:%8.2lf %s"  \
+GPRINT:wifi:MIN:"Minimum\:%8.2lf %s"  \
+GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
+#-------------------------------- DAY ----------------------------------#
+rrdtool graph $RRDPATH/day.png --title="Day Period of WiFi Signal Quality" --start -1d \
+--height=240 \
+--width=640 \
+--lower-limit=0 \
+--vertical-label='quality' \
+--font TITLE:12: \
+--font AXIS:8: \
+--font LEGEND:10: \
+--font UNIT:8: \
+DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
+LINE:wifi$REDD:"%" \
+AREA:wifi$REDL \
+GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s"  \
+GPRINT:wifi:MIN:"Minimum\:%8.2lf %s"  \
+GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
+#-------------------------------- WEEK ---------------------------------#
+rrdtool graph $RRDPATH/week.png --title="Week Period of WiFi Signal Quality" --start -1w \
+--height=240 \
+--width=640 \
+--lower-limit=0 \
+--vertical-label='quality' \
+--font TITLE:12: \
+--font AXIS:8: \
+--font LEGEND:10: \
+--font UNIT:8: \
+DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
+LINE:wifi$REDD:"%" \
+GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s"  \
+GPRINT:wifi:MIN:"Minimum\:%8.2lf %s"  \
+GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
+#------------------------------- MONTH ---------------------------------#
+rrdtool graph $RRDPATH/month.png --title="Month Period of WiFi Signal Quality" --start -1m \
+--height=240 \
+--width=640 \
+--lower-limit=0 \
+--vertical-label='quality' \
+--font TITLE:12: \
+--font AXIS:8: \
+--font LEGEND:10: \
+--font UNIT:8: \
+DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
+LINE:wifi$REDD:"%" \
+GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s"  \
+GPRINT:wifi:MIN:"Minimum\:%8.2lf %s"  \
+GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
+#-------------------------------- YEAR ---------------------------------#
+rrdtool graph $RRDPATH/year.png --title="Year Period of WiFi Signal Quality" --start -1y \
+--height=240 \
+--width=640 \
+--lower-limit=0 \
+--vertical-label='quality' \
+--font TITLE:12: \
+--font AXIS:8: \
+--font LEGEND:10: \
+--font UNIT:8: \
+DEF:wifi=$RRDPATH/wifi.rrd:wifi:AVERAGE \
+LINE:wifi$REDD:"%" \
+GPRINT:wifi:AVERAGE:"Average\:%8.2lf %s"  \
+GPRINT:wifi:MIN:"Minimum\:%8.2lf %s"  \
+GPRINT:wifi:MAX:"Maximum\:%8.2lf %s\n" > /dev/null 2>&1
+}
+
+#-------------------------------- CORE ---------------------------------#
+clear
+interfaces
+address
+signal
+latency
+graphing
+report