![]() |
![]() |
![]() |
![]() |
![]() |
This script solves some problems in wireless networks that lose connection. It's tested in Slackware 13.37 and 14. It uses wpa_supplicant and works with multiple SSID and multiple IP configuration, checks wifi status every 5 seconds and resets wireless system if not working. Wireless configuration is stored in a text file named "wifi.conf", you can define from 1 to 3 SSID with static IP or DHCP. This is a typical wifi.conf file:
ESSID[1]=office USER[1]=job_user PASS[1]=job_pass CRYPT[1]=radius PRIORITY[1]=5 HIDDEN[1]=yes DHCP[1]=no ESSID[2]=myhome USER[2]=null PASS[2]=my_password CRYPT[2]=wpa PRIORITY[2]=3 HIDDEN[2]=no DHCP[2]=yes ESSID[3]=mybar USER[3]=null PASS[3]=bar_password CRYPT[3]=wpa PRIORITY[3]=1 HIDDEN[3]=no DHCP[3]=yes ETH=eth0 WLAN=wlan0 IP=192.168.0.20 MK=255.255.255.0 GW=192.168.0.254 DNS=8.8.8.8 PING_TEST=yes
Make "/root/wifi" directory, then copy and paste the sorce code in a file named "wifi-connect.sh". Put this file and the file "wifi.conf" into /root/wifi/ folder.
wifi-connect.sh source code: |
---|
#!/bin/sh # wifi-connect.sh r.0.9, 2017-06-18 # added automatic switch wlan<->eth # added 1 second delay after kill wpa_supplicant (patch reconnection problems) CWD=/root/wifi #CWD=`pwd` CONF=$CWD/wifi.conf LOGFILE=$CWD/wifi-log WPA_CONFIG=$CWD/wpa_supplicant.conf NETWORK=$CWD/network.conf ETH=eth0 WLAN=wlan0 . $CONF #Input: CRIPT ESSID USER PASS PRIORITY HIDDEN function make_network { case $1 in "wpa") /usr/sbin/wpa_passphrase $2 "$4" > $NETWORK sed -i '/}/i \\t\scan_ssid=1' $NETWORK sed -i "/}/i \\\tpriority=$5" $NETWORK ;; "radius") echo " network={ ssid=\"$2\" scan_ssid=1 priority=$5 key_mgmt=WPA-EAP eap=PEAP #PEAP or TTLS permitted phase1=\"peaplabel=0\" #Force MSCHAPV2 phase2=\"auth=GTC\" #Force MSCHAPV2 identity=\"$3\" password=\"$4\" } " > $NETWORK ;; esac if [ "a$6" == "ayes" ] ; then ADDCONFIG=`awk 'NR==3{$0="\ scan_ssid=1\ \n key_mgmt=WPA-PSK\ \n group=TKIP CCMP\ \n pairwise=TKIP CCMP\ \n proto=WPA WPA2\ \n"$0}1' $NETWORK` IFS=" " echo $ADDCONFIG > $NETWORK fi } cp -f $LOGFILE.4.txt $LOGFILE.5.txt &> /dev/null cp -f $LOGFILE.3.txt $LOGFILE.4.txt &> /dev/null cp -f $LOGFILE.2.txt $LOGFILE.3.txt &> /dev/null cp -f $LOGFILE.1.txt $LOGFILE.2.txt &> /dev/null cp -f $LOGFILE.0.txt $LOGFILE.1.txt &> /dev/null rm -f $LOGFILE.0.txt rm -f $WPA_CONFIG IFS= # ---- Make wpa_supplicant conf file ---- for N in 1 2 3 do if [ "a${ESSID[$N]}" != "a" ] ; then make_network ${CRYPT[$N]} ${ESSID[$N]} ${USER[$N]} ${PASS[$N]} ${PRIORITY[$N]} ${HIDDEN[$N]} cat $NETWORK >> $WPA_CONFIG else ESSID[$N]=null fi done # ---- kill wpa_supplicant and start system ---- killall -s 9 wpa_supplicant &> /dev/null sleep 1 echo "`date +"%Y-%m-%d:%H.%M.%S"` wifi-connect.sh started ..." >> $LOGFILE.0.txt # ---- Restore wlan interface ---- rfkill unblock wlan sleep 1 ifconfig $WLAN up &> /dev/null PING_FAILURE=0 IP_RENEW=no ASSOCIATED_ID="off/any" while (true) do ifconfig $ETH up ETH_STATE=`ethtool eth0 | grep "Link detected" | cut -d":" -f2 | cut -d" " -f2` if [ "a$ETH_STATE" == "ayes" ] ; then ETH_HAVE_IP=`ifconfig $ETH | grep 'inet '` if [ "a$ETH_HAVE_IP" == "a" ] ; then echo "`date +"%Y-%m-%d:%H.%M.%S"` $ETH up, configure cable connection ..." >> $LOGFILE.0.txt killall -s 2 wpa_supplicant &> /dev/null rfkill unblock wlan ifconfig $WLAN down &> /dev/null ifconfig $ETH $IP netmask $MK &> /dev/null route add default gw $GW &> /dev/null echo "nameserver $DNS" > /etc/resolv.conf fi else ip addr flush dev $ETH PREC_SSID=$ASSOCIATED_ID ASSOCIATED_ID=`iwconfig wlan0 | grep ESSID | cut -d: -f2 | sed 's/"//g'| sed 's/ //g'` if [ "a$ASSOCIATED_ID" == "aoff/any" ] ; then echo "`date +"%Y-%m-%d:%H.%M.%S"` not associated, try to reconnect wifi ..." >> $LOGFILE.0.txt killall -s 2 wpa_supplicant &> /dev/null sleep 1 rfkill unblock wlan ifconfig $WLAN up &> /dev/null wpa_supplicant -B -Dwext -i$WLAN -c $WPA_CONFIG 2>&1 >> $LOGFILE.0.txt fi # ---- Switch to other ssid? ---- if [ "a$PREC_SSID" != "a$ASSOCIATED_ID" ] ; then IP_RENEW=yes fi # ---- If associated AP, then start network configuration ---- if [ "a$IP_RENEW" == "ayes" ] ; then DHCP=yes for N in 1 2 3 do if [ "a$ASSOCIATED_ID" == "a${ESSID[$N]}" ] ; then DHCP=${DHCP[$N]} IP_RENEW=work echo "`date +"%Y-%m-%d:%H.%M.%S"` ssid=$ASSOCIATED_ID" >> $LOGFILE.0.txt fi done fi # ---- If reset network or change ssid then renew ip configuration ---- if [ "a$IP_RENEW" == "awork" ] ; then if [ "a$DHCP" == "ayes" ] ; then echo "`date +"%Y-%m-%d:%H.%M.%S"` using DHCP ..." >> $LOGFILE.0.txt dhcpcd -x &> /dev/null dhcpcd -k $WLAN &> /dev/null sleep 1 ip addr flush dev $WLAN dhcpcd $WLAN &>> $LOGFILE.0.txt else dhcpcd -x &> /dev/null sleep 1 echo "`date +"%Y-%m-%d:%H.%M.%S"` add default gw $GW ..." >> $LOGFILE.0.txt ifconfig $WLAN $IP netmask $MK &> /dev/null route add default gw $GW &> /dev/null echo "nameserver $DNS" > /etc/resolv.conf fi IP_RENEW=done fi # ---- Continuous ping test ---- if [ "a$PING_TEST" == "ayes" ] && [ "a$IP_RENEW" == "adone" ] ; then GATEWAY=`ip route show | grep 'default' | awk '{print $3}' | cut -d$'\012' -f1` if [ "a$GATEWAY" != "a" ] ; then CONNECT=`ping -c 1 $GATEWAY 2>/dev/null | grep "1 received"` if [ "a$CONNECT" == "a" ] ; then let PING_FAILURE=$PING_FAILURE+1 else PING_FAILURE=0 fi if [ $PING_FAILURE -gt 5 ] ; then echo "`date +"%Y-%m-%d:%H.%M.%S"` too many LAN ping errors, reset network ..." >> $LOGFILE.0.txt killall -s 2 wpa_supplicant &> /dev/null sleep 1 rfkill unblock wlan ifconfig $WLAN up &> /dev/null IP_RENEW=no PING_FAILURE=0 fi else echo "`date +"%Y-%m-%d:%H.%M.%S"` no route, restart inet ..." >> $LOGFILE.0.txt IP_RENEW=work fi fi # ping test fi # eth_state sleep 5 done exit |
To start the script in background in Slackware, add this code to /etc/rc.d/rc.local
#wlan connect if [ -x /root/wifi/wifi-connect.sh ]; then /root/wifi/wifi-connect.sh & fi
Note: to avoid software interference, disable any wireless daemon as example wicd or wifi-manager.
This software is relased "as is", without any warranty, use at own risk.