125 lines
3.6 KiB
Bash
Executable File
125 lines
3.6 KiB
Bash
Executable File
#! /bin/sh
|
|
###################################################################################
|
|
# Choose and enable wifi-interface with uci
|
|
# Usage: choose_wifi.sh [ssid]
|
|
# when argument is passed, script tries to activate wifi interface with that ssid
|
|
# else script tries to activate first match from iwlist scanning results
|
|
#
|
|
# author: Christian Ulrich
|
|
# version: 0.1
|
|
###################################################################################
|
|
|
|
|
|
# function search_ssid( ssid, number_of_ifaces )
|
|
#################################################
|
|
search_ssid(){
|
|
found=false
|
|
current=0
|
|
index_to_return=0
|
|
while [ $found = false -a $current -lt $2 ]
|
|
do
|
|
uci_result=`uci get wireless.@wifi-iface[$current].ssid 2>&1`
|
|
if [ $uci_result = $1 ]; then
|
|
index_to_return=`expr $current + 1`
|
|
found=true
|
|
fi
|
|
current=`expr $current + 1` #increment counter
|
|
done
|
|
return $index_to_return
|
|
}
|
|
|
|
# Count interfaces in /etc/config/wireless (result in $iface_counter)
|
|
# and check if any of them is enabled
|
|
######################################################################
|
|
uci_result="0"
|
|
value_exists=true
|
|
iface_counter=0
|
|
enabled_iface_exists=false
|
|
while [ $value_exists = true ]
|
|
do
|
|
uci_result=`uci get wireless.@wifi-iface[$iface_counter].ssid 2>&1`
|
|
if [ $? -eq 0 ]; then
|
|
#iface_counter=`expr $iface_counter + 1` #increment iface_counter
|
|
#uci_result=`uci get wireless.@wifi-iface[$iface_counter].disabled 2>/dev/null`
|
|
if [ `uci get wireless.@wifi-iface[$iface_counter].disabled 2>/dev/null` = 0 ]; then
|
|
enabled_iface_exists=true
|
|
fi
|
|
iface_counter=`expr $iface_counter + 1` #increment iface_counter
|
|
else
|
|
value_exists=false
|
|
fi
|
|
done
|
|
|
|
# Get Wifi interface
|
|
#####################
|
|
#wifi_iface=$(cat /proc/net/wireless | grep : )
|
|
#wifi_iface=$(echo $wifi_iface | cut -c1-$(expr $(expr index "$wifi_iface" :) - 1 ))
|
|
|
|
# Check commandline options and start searching
|
|
################################################
|
|
if [ $# -eq 0 ]; then
|
|
if [ $enabled_iface_exists = false ]; then # if no interface is enabled, enable first one
|
|
uci set wireless.@wifi-iface[0].disabled=0
|
|
/etc/init.d/network reload
|
|
sleep 1
|
|
fi
|
|
|
|
#scan_result=`iwlist ${wifi_iface} scanning | grep ESSID`
|
|
scan_result=`iwlist scanning 2>/dev/null | grep ESSID`
|
|
if [ $? -eq 1 ]; then
|
|
sleep 1
|
|
scan_result=`iwlist scanning 2>/dev/null | grep ESSID`
|
|
fi
|
|
#echo ${scan_result[1]}
|
|
scan_result=`echo $scan_result | tr -d 'ESSID:"'`
|
|
#scan_result=`echo $scan_result | tr -d '" '`
|
|
|
|
index_of_ssid_to_activate=0
|
|
for j in $scan_result
|
|
do
|
|
if [ $index_of_ssid_to_activate -eq 0 ]; then
|
|
search_ssid $j $iface_counter
|
|
index_of_ssid_to_activate=$?
|
|
if [ $index_of_ssid_to_activate -ne 0 ]; then
|
|
#ssid=$j
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
|
|
elif [ $# -eq 1 ]; then
|
|
index_of_ssid_to_activate=0
|
|
search_ssid $1 $iface_counter
|
|
index_of_ssid_to_activate=$?
|
|
|
|
else
|
|
echo "usage: choose_wifi.sh [ssid]"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# Evaluate results and enable the right interface with uci
|
|
###########################################################
|
|
|
|
if [ $index_of_ssid_to_activate -eq 0 ]; then
|
|
echo "Network with SSID ${1} not defined in /etc/config/wireless."
|
|
exit 1
|
|
fi
|
|
|
|
i=1
|
|
while [ $i -le $iface_counter ]
|
|
do
|
|
if [ $i -eq $index_of_ssid_to_activate ]; then
|
|
uci set wireless.@wifi-iface[`expr $i - 1`].disabled=0
|
|
ssid=$(uci get wireless.@wifi-iface[`expr $i - 1`].ssid)
|
|
else
|
|
uci set wireless.@wifi-iface[`expr $i - 1`].disabled=1
|
|
fi
|
|
i=`expr $i + 1`
|
|
done
|
|
|
|
# Activate interface
|
|
##########################################
|
|
echo "Activating wifi-interface ${ssid}..."
|
|
/etc/init.d/network reload
|