(add default ETH and WLAN)
(rev.0.9)
 
Line 1: Line 1:
<source lang="bash">
<source lang="bash">
#!/bin/sh
#!/bin/sh
-
# wifi-connect.sh r.0.8, 2014-04-24
+
# wifi-connect.sh r.0.9, 2017-06-18
# added automatic switch wlan<->eth
# added automatic switch wlan<->eth
# added 1 second delay after kill wpa_supplicant (patch reconnection problems)
# added 1 second delay after kill wpa_supplicant (patch reconnection problems)
-
 
+
CWD=/root/wifi
CWD=/root/wifi
#CWD=`pwd`
#CWD=`pwd`
-
 
+
CONF=$CWD/wifi.conf
CONF=$CWD/wifi.conf
LOGFILE=$CWD/wifi-log
LOGFILE=$CWD/wifi-log
WPA_CONFIG=$CWD/wpa_supplicant.conf
WPA_CONFIG=$CWD/wpa_supplicant.conf
NETWORK=$CWD/network.conf
NETWORK=$CWD/network.conf
-
 
+
ETH=eth0
ETH=eth0
WLAN=wlan0
WLAN=wlan0
-
 
+
. $CONF
. $CONF
-
 
+
#Input: CRIPT ESSID USER PASS PRIORITY HIDDEN
#Input: CRIPT ESSID USER PASS PRIORITY HIDDEN
function make_network {
function make_network {
-
 
+
case $1 in
case $1 in
-
 
+
  "wpa") /usr/sbin/wpa_passphrase $2 "$4" > $NETWORK
  "wpa") /usr/sbin/wpa_passphrase $2 "$4" > $NETWORK
sed -i '/}/i \\t\scan_ssid=1' $NETWORK
sed -i '/}/i \\t\scan_ssid=1' $NETWORK
Line 43: Line 43:
  ;;
  ;;
esac
esac
-
 
+
if [ "a$6" == "ayes" ] ; then
if [ "a$6" == "ayes" ] ; then
-
 
+
ADDCONFIG=`awk 'NR==3{$0="\
ADDCONFIG=`awk 'NR==3{$0="\
scan_ssid=1\
scan_ssid=1\
Line 53: Line 53:
\n proto=WPA WPA2\
\n proto=WPA WPA2\
\n"$0}1' $NETWORK`
\n"$0}1' $NETWORK`
-
 
+
IFS=" "
IFS=" "
echo $ADDCONFIG > $NETWORK
echo $ADDCONFIG > $NETWORK
-
 
+
fi
fi
}
}
Line 65: Line 65:
cp -f $LOGFILE.1.txt $LOGFILE.2.txt &> /dev/null
cp -f $LOGFILE.1.txt $LOGFILE.2.txt &> /dev/null
cp -f $LOGFILE.0.txt $LOGFILE.1.txt &> /dev/null
cp -f $LOGFILE.0.txt $LOGFILE.1.txt &> /dev/null
-
 
+
rm -f $LOGFILE.0.txt
rm -f $LOGFILE.0.txt
rm -f $WPA_CONFIG
rm -f $WPA_CONFIG
-
 
+
IFS=
IFS=
# ---- Make wpa_supplicant conf file ----
# ---- Make wpa_supplicant conf file ----
Line 80: Line 80:
  fi
  fi
done
done
-
 
+
# ---- kill wpa_supplicant and start system ----
# ---- kill wpa_supplicant and start system ----
killall -s 9 wpa_supplicant &> /dev/null
killall -s 9 wpa_supplicant &> /dev/null
sleep 1
sleep 1
echo "`date +"%Y-%m-%d:%H.%M.%S"` wifi-connect.sh started ..." >> $LOGFILE.0.txt
echo "`date +"%Y-%m-%d:%H.%M.%S"` wifi-connect.sh started ..." >> $LOGFILE.0.txt
-
 
+
# ---- Restore wlan interface ----
# ---- Restore wlan interface ----
rfkill unblock wlan
rfkill unblock wlan
sleep 1
sleep 1
ifconfig $WLAN up &> /dev/null
ifconfig $WLAN up &> /dev/null
-
 
+
PING_FAILURE=0
PING_FAILURE=0
IP_RENEW=no
IP_RENEW=no
ASSOCIATED_ID="off/any"
ASSOCIATED_ID="off/any"
-
 
+
while (true) do
while (true) do
-
 
+
  ifconfig $ETH up
  ifconfig $ETH up
-
  ETH_STATE=`cat /sys/class/net/$ETH/operstate`
+
  ETH_STATE=`ethtool eth0 | grep "Link detected" | cut -d":" -f2 | cut -d" " -f2`
-
 
+
-
  if [ "a$ETH_STATE" == "aup" ] ; then
+
  if [ "a$ETH_STATE" == "ayes" ] ; then
   ETH_HAVE_IP=`ifconfig $ETH | grep 'inet '`
   ETH_HAVE_IP=`ifconfig $ETH | grep 'inet '`
-
 
+
   if [ "a$ETH_HAVE_IP" == "a" ] ; then
   if [ "a$ETH_HAVE_IP" == "a" ] ; then
   echo "`date +"%Y-%m-%d:%H.%M.%S"` $ETH up, configure cable connection ..." >> $LOGFILE.0.txt
   echo "`date +"%Y-%m-%d:%H.%M.%S"` $ETH up, configure cable connection ..." >> $LOGFILE.0.txt
Line 108: Line 108:
   rfkill unblock wlan
   rfkill unblock wlan
   ifconfig $WLAN down &> /dev/null
   ifconfig $WLAN down &> /dev/null
-
 
+
   ifconfig $ETH $IP netmask $MK &> /dev/null  
   ifconfig $ETH $IP netmask $MK &> /dev/null  
   route add default gw $GW &> /dev/null
   route add default gw $GW &> /dev/null
   echo "nameserver $DNS" > /etc/resolv.conf
   echo "nameserver $DNS" > /etc/resolv.conf
   fi
   fi
-
 
+
  else
  else
-
 
+
  ip addr flush dev $ETH
  ip addr flush dev $ETH
-
 
+
  PREC_SSID=$ASSOCIATED_ID
  PREC_SSID=$ASSOCIATED_ID
-
 
+
  ASSOCIATED_ID=`iwconfig wlan0 | grep ESSID | cut -d: -f2 | sed 's/"//g'| sed 's/ //g'`
  ASSOCIATED_ID=`iwconfig wlan0 | grep ESSID | cut -d: -f2 | sed 's/"//g'| sed 's/ //g'`
-
 
+
  if [ "a$ASSOCIATED_ID" == "aoff/any" ] ; then
  if [ "a$ASSOCIATED_ID" == "aoff/any" ] ; then
-
 
+
   echo "`date +"%Y-%m-%d:%H.%M.%S"` not associated, try to reconnect wifi ..." >> $LOGFILE.0.txt
   echo "`date +"%Y-%m-%d:%H.%M.%S"` not associated, try to reconnect wifi ..." >> $LOGFILE.0.txt
   killall -s 2 wpa_supplicant &> /dev/null
   killall -s 2 wpa_supplicant &> /dev/null
Line 129: Line 129:
   rfkill unblock wlan
   rfkill unblock wlan
   ifconfig $WLAN up &> /dev/null
   ifconfig $WLAN up &> /dev/null
-
 
+
   wpa_supplicant -B -Dwext -i$WLAN -c $WPA_CONFIG 2>&1 >> $LOGFILE.0.txt
   wpa_supplicant -B -Dwext -i$WLAN -c $WPA_CONFIG 2>&1 >> $LOGFILE.0.txt
-
 
+
  fi
  fi
-
 
+
  # ---- Switch to other ssid? ----
  # ---- Switch to other ssid? ----
  if [ "a$PREC_SSID" != "a$ASSOCIATED_ID" ] ; then
  if [ "a$PREC_SSID" != "a$ASSOCIATED_ID" ] ; then
   IP_RENEW=yes
   IP_RENEW=yes
  fi
  fi
-
 
+
  # ---- If associated AP, then start network configuration ----
  # ---- If associated AP, then start network configuration ----
  if [ "a$IP_RENEW" == "ayes" ] ; then
  if [ "a$IP_RENEW" == "ayes" ] ; then
-
 
+
   DHCP=yes
   DHCP=yes
   for N in 1 2 3
   for N in 1 2 3
Line 152: Line 152:
   done
   done
  fi
  fi
-
 
+
  # ---- If reset network or change ssid then renew ip configuration ----
  # ---- If reset network or change ssid then renew ip configuration ----
  if [ "a$IP_RENEW" == "awork" ] ; then
  if [ "a$IP_RENEW" == "awork" ] ; then
-
 
+
   if [ "a$DHCP" == "ayes" ] ; then
   if [ "a$DHCP" == "ayes" ] ; then
   echo "`date +"%Y-%m-%d:%H.%M.%S"` using DHCP ..." >> $LOGFILE.0.txt
   echo "`date +"%Y-%m-%d:%H.%M.%S"` using DHCP ..." >> $LOGFILE.0.txt
Line 163: Line 163:
   ip addr flush dev $WLAN
   ip addr flush dev $WLAN
   dhcpcd $WLAN  &>> $LOGFILE.0.txt
   dhcpcd $WLAN  &>> $LOGFILE.0.txt
-
 
+
   else
   else
   dhcpcd -x &> /dev/null
   dhcpcd -x &> /dev/null
Line 171: Line 171:
   route add default gw $GW &> /dev/null
   route add default gw $GW &> /dev/null
   echo "nameserver $DNS" > /etc/resolv.conf
   echo "nameserver $DNS" > /etc/resolv.conf
-
 
+
   fi
   fi
-
 
+
   IP_RENEW=done
   IP_RENEW=done
  fi
  fi
-
 
+
  # ---- Continuous ping test ----
  # ---- Continuous ping test ----
  if [ "a$PING_TEST" == "ayes" ] && [ "a$IP_RENEW" == "adone" ] ; then
  if [ "a$PING_TEST" == "ayes" ] && [ "a$IP_RENEW" == "adone" ] ; then
   GATEWAY=`ip route show | grep 'default' | awk '{print $3}' | cut -d$'\012' -f1`
   GATEWAY=`ip route show | grep 'default' | awk '{print $3}' | cut -d$'\012' -f1`
-
 
+
   if [ "a$GATEWAY" != "a" ] ; then
   if [ "a$GATEWAY" != "a" ] ; then
-
 
+
   CONNECT=`ping -c 1 $GATEWAY 2>/dev/null | grep "1 received"`
   CONNECT=`ping -c 1 $GATEWAY 2>/dev/null | grep "1 received"`
-
 
+
   if [ "a$CONNECT" == "a" ] ; then
   if [ "a$CONNECT" == "a" ] ; then
     let PING_FAILURE=$PING_FAILURE+1
     let PING_FAILURE=$PING_FAILURE+1
Line 190: Line 190:
     PING_FAILURE=0
     PING_FAILURE=0
   fi
   fi
-
 
+
   if [ $PING_FAILURE -gt 5 ] ; then
   if [ $PING_FAILURE -gt 5 ] ; then
     echo "`date +"%Y-%m-%d:%H.%M.%S"` too many LAN ping errors, reset network ..." >> $LOGFILE.0.txt
     echo "`date +"%Y-%m-%d:%H.%M.%S"` too many LAN ping errors, reset network ..." >> $LOGFILE.0.txt
Line 200: Line 200:
     PING_FAILURE=0
     PING_FAILURE=0
   fi
   fi
-
 
+
   else
   else
   echo "`date +"%Y-%m-%d:%H.%M.%S"` no route, restart inet ..." >> $LOGFILE.0.txt
   echo "`date +"%Y-%m-%d:%H.%M.%S"` no route, restart inet ..." >> $LOGFILE.0.txt
   IP_RENEW=work
   IP_RENEW=work
-
 
+
   fi
   fi
-
 
+
  fi # ping test
  fi # ping test
-
 
+
  fi # eth_state
  fi # eth_state
  sleep 5
  sleep 5
-
 
+
done
done
-
 
+
exit
exit
 +
</source>
</source>
<noinclude>[[Category:Source code]]</noinclude>
<noinclude>[[Category:Source code]]</noinclude>

Latest revision as of 19:37, 19 June 2017

#!/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
Retrieved from "http://www.zoros.org/wiki/index.php?title=Wifi-connect.sh_source_code"