#!/bin/sh -x wifi=wlan0 iface=@wifi-iface[0] radio=$(uci get wireless.${iface}.device) connect_wifi(){ # channel ssid encryption key uci set wireless.${iface}.mode=sta ifconfig $wifi up uci set wireless.${radio}.channel=$1 uci set "wireless.${iface}.ssid=$2" if [ $3 == "none" ] ; then uci set wireless.${iface}.encryption=none uci -q delete wireless.${iface}.key else uci set "wireless.${iface}.key=$4" uci set wireless.${iface}.encryption=$3 fi uci commit wireless wifi up } iwlist_scan(){ count=0 iwlist scan ${1:-} 2>/dev/null | ( while read line; do case "$line" in *"Cell "*) #echo : $((count+=1)) echo MAC_${count}="${line#*Address: }" ;; *Channel:*) echo CHANNEL_${count}="${line#*:}" ;; *Quality=*) echo QUALITY_${count}="`printf '%s' ${line#*Quality=} | cut -d/ -f 1`" ;; *"Encryption key:"*) echo ENCRYPTION_${count}="${line#*key:}" ;; *ESSID:*) echo ESSID_${count}="${line#*ESSID:}" ;; *"IE: IEEE 802.11i/WPA2"*) echo WPA2_${count}=1 ;; *"IE: WPA Version 1"*) echo WPA_${count}=1 ;; *);; esac done; echo WIFI_COUNT=$count) } iwlist_scan > /tmp/${wifi}.scan connect_wifi "$@"