Skip to content

Instantly share code, notes, and snippets.

@dan82840
Last active November 3, 2016 03:32
Show Gist options
  • Save dan82840/23aaa4df8fd615745c83e981bf52b202 to your computer and use it in GitHub Desktop.
Save dan82840/23aaa4df8fd615745c83e981bf52b202 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# ./test-wireless.sh [number of run]
#
SSID1=
SSID2=
LOG_FILE=/tmp/wireless.log
. /lib/functions.sh
ECHO() {
echo "$1"
if [ ! -z $LOG_FILE ]; then
echo "$1" >> $LOG_FILE
fi
}
usage() {
echo "./test-wireless.sh [number of run]"
}
# $1: number of arguments
# $2: number of run
check_args() {
local total=$1
local run=$2
[ "$total" != "1" ] && {
usage
exit 1
}
}
# $1: wireless interface name
# $2: SSID name
add_ssid() {
local iface=$1
local ssid=$2
ECHO "Adding $ssid to $iface !!!"
uci add wireless wifi-iface >/dev/null 2>&1
uci set wireless.@wifi-iface[-1].device="$iface"
uci set wireless.@wifi-iface[-1].network='lan'
uci set wireless.@wifi-iface[-1].mode='ap'
uci set wireless.@wifi-iface[-1].ssid="$ssid"
uci set wireless.@wifi-iface[-1].encryption='none'
uci set wireless.@wifi-iface[-1]._mark='1'
}
# $1: index
new_ssid() {
local index=$1
local disabled
[ -z $SSID1 ] && {
SSID1=$(uci get wireless.@wifi-iface[0].ssid)
}
[ -z $SSID2 ] && {
disabled=$(uci get wireless.@wifi-iface[1].disabled)
if [ "$disabled" == "1" ]; then
SSID2=$(uci get wireless.@wifi-iface[2].ssid)
else
SSID2=$(uci get wireless.@wifi-iface[1].ssid)
fi
}
[ "$index" != "15" ] && {
add_ssid "wifi0" $SSID1"_"$index
}
add_ssid "wifi1" $SSID2"_"$index
ECHO "Save wireless setting !!!"
uci commit wireless
}
# $1: section name
del_ssid_cb() {
local section=$1
config_get _mark $section _mark
[ "$_mark" == "1" ] && {
ECHO "Deleting wireless.$section !!!"
uci delete wireless.$section
}
}
del_all_ssid() {
ECHO "Delete all SSID we creates !!!"
config_load wireless
ECHO "del_ssid_cb !!!"
config_foreach del_ssid_cb wifi-iface
uci commit wireless
}
# $1: section name
# $2: index
add_ssid_cb() {
local section=$1
local index=$2
config_get device $section device
if [ "$device" == "wifi0" ]; then
[ -z $SSID1 ] && config_get SSID1 $section ssid
[ "$section" == "guest" ] && return
for i in $(seq 1 $index); do
[ "$i" == "15" ] && break
add_ssid "wifi0" $SSID1"_"$i
done
elif [ "$device" == "wifi1" ]; then
[ -z $SSID2 ] && config_get SSID2 $section ssid
for i in $(seq 1 $index); do
add_ssid "wifi1" $SSID2"_"$i
done
fi
}
# $1: index
add_all_ssid() {
local index=$1
config_load wireless
config_foreach add_ssid_cb wifi-iface $index
uci commit wireless
}
# $1: ssid to check
# $2: return variable
is_ssid_up() {
local ssid=$1
local retval=$2
local count=0
local hit=0
local inf _ssid
local line_num=$(cat /proc/net/wireless | wc -l)
[ "$line_num" == "2" ] && {
eval "export -- \"$retval=\"0\"\""
return
}
IFS=$'\n'
for line in $(cat /proc/net/wireless); do
count=$((count + 1))
[ "$count" == "1" -o "$count" == "2" ] && continue #skip 1st and 2nd line
inf=$(echo $line | awk -F":" '{print $1}' | sed 's/ //g')
[ "$inf" == "wifi0" -o "$inf" == "wifi1" ] && continue
_ssid=$(iwconfig $inf | awk -F '"' '/ESSID/ {print $2}')
if [ "$ssid" == "$_ssid" ]; then
hit=1
break
fi
done
eval "export -- \"$retval=\"\"$hit\"\"\""
}
# $1: section name
check_ssid_up() {
local section=$1
config_get ssid $section ssid
config_get disabled $section disabled
[ "$disabled" == "1" ] && {
ECHO "SSID $ssid DISABLED !!!"
return
}
is_ssid_up "$ssid" "rc"
if [ "$rc" == "0" ]; then
ECHO "Checking SSID $ssid !!!"
sleep 5
check_ssid_up $section
else
ECHO "SSID $ssid UP !!!"
fi
}
# $1: section name
check_ssid_cb() {
local section=$1
check_ssid_up $section
}
check_all_ssid() {
ECHO "Checking ALL SSID !!!"
config_load wireless
config_foreach check_ssid_cb wifi-iface
}
reload_wireless() {
ECHO "Reloading wireless !!!"
/sbin/reload_config wireless
}
# $1: number of run
main() {
local run=$1
rm -f $LOG_FILE
del_all_ssid
for run in $(seq 1 $run); do
ECHO "==== RUN $run ===="
for index in $(seq 1 15); do
ECHO "==== Index $index ===="
#add_all_ssid $index
new_ssid $index
reload_wireless
check_all_ssid
done
done
}
check_args $# $1
main $1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment