#!/bin/sh
# wifi-connect.sh r.0.8, 2014-04-24
# 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=`cat /sys/class/net/$ETH/operstate`
 
 if [ "a$ETH_STATE" == "aup" ] ; 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
Retrieved from "http://www.zoros.org/wiki/index.php?title=Wifi-connect.sh_source_code"