# HG changeset patch # User Christophe Lincoln # Date 1235336626 -3600 # Node ID 0a481e5dd003c22a9ff0fd0a9c70458572928a53 # Parent f390590b62b46e0a324b6fe2a2a61829336fd4b8 Add WifiBox - Manage network wireless (essids, favorites, drivers) diff -r f390590b62b4 -r 0a481e5dd003 rootfs/usr/share/applications/wifibox.desktop --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rootfs/usr/share/applications/wifibox.desktop Sun Feb 22 22:03:46 2009 +0100 @@ -0,0 +1,8 @@ +[Desktop Entry] +Encoding=UTF-8 +Name=Wireless networks connections +Name[fr]=Connexions réseaux sans fil +Exec=subox wifibox +Icon=network-wireless +Type=Application +Categories=System;Application; diff -r f390590b62b4 -r 0a481e5dd003 tinyutils/wifibox --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tinyutils/wifibox Sun Feb 22 22:03:46 2009 +0100 @@ -0,0 +1,618 @@ +#!/bin/sh +# +# GTKdialog interface to manage wireless connection in a simple way. +# Use tabs to indent, split commands from the GUI and use functions. +# Favotites networks are also supported +# +# (c) 2009 SliTaz GNU/Linux - GNU gpl v3 +# + +# Export script path and other if needed so we can use them in 'quote'. +export BIN=$0 +export FAVORITES_WIFI=/etc/wireless +. /etc/network.conf + +# Wifibox is only for root. +if test $(id -u) != 0 ; then + exec subox wifibox + exit 0 +fi + +# Sanity check +[ -x /usr/sbin/iwconfig ] || tazpkg get-install wireless_tools +[ -d $FAVORITES_WIFI ] || mkdir -p $FAVORITES_WIFI +rm -f $FAVORITES_WIFI/any.conf + +# Catch ESSIDs and format output for GTK tree. We get the list off +# network by Cell and without space. +detect_wifi_networks() +{ + desktopbox notify "Scanning Wireless interface: $WIFI_INTERFACE" & + if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then + ifconfig $WIFI_INTERFACE up + for i in `iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}'` + do + SCAN=`iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep -A 8 "$i"` + ESSID=`echo $SCAN | cut -d '"' -f 2` + if echo "$SCAN" | grep -q Quality; then + QUALITY=`echo $SCAN | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/'` + else + QUALITY="-" + fi + ENCRYPTION=`echo $SCAN | sed 's/.*key:\([^ ]*\).*/\1/'` + # Check encryption type + if echo "$SCAN" | grep -q WPA; then + ENCRYPTION="${ENCRYPTION} (WPA)" + fi + # Connected or not connected... + if ifconfig | grep -A 1 $WIFI_INTERFACE | \ + grep -q inet && iwconfig $WIFI_INTERFACE | \ + grep ESSID | grep -q -w "$ESSID"; then + STATUS=connected + else + STATUS="-" + fi + echo -n "" + echo "$ESSID | $QUALITY | $ENCRYPTION | $STATUS" + done + fi +} + +# cmdline functions + +# Configure /etc/network.conf and restart connexion with init script. +start_wifi_connection() +{ + # Get tmp config created by connect_to_essid() if exist and set + # empty value to clean config file. + if [ -f /tmp/wifi.conf ]; then + . /tmp/wifi.conf + WIFI_MODE="" + WIFI_IWCONFIG_ARGS="" + WIFI_CHANNEL="" + fi + sed -i "s/`grep ^WIFI= /etc/network.conf`/WIFI=\"yes\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_INTERFACE= /etc/network.conf`/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_ESSID= /etc/network.conf`/WIFI_ESSID=\"$WIFI_ESSID\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_KEY= /etc/network.conf`/WIFI_KEY=\"$WIFI_KEY\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_MODE= /etc/network.conf`/WIFI_MODE=\"$WIFI_MODE\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_IWCONFIG_ARGS= /etc/network.conf`/WIFI_IWCONFIG_ARGS=\"$WIFI_IWCONFIG_ARGS\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_KEY_TYPE= /etc/network.conf`/WIFI_KEY_TYPE=\"$WIFI_KEY_TYPE\"/" \ + /etc/network.conf + sed -i "s/`grep ^WIFI_CHANNEL= /etc/network.conf`/WIFI_CHANNEL=\"$WIFI_CHANNEL\"/" \ + /etc/network.conf + [ -s /var/run/udhcpc.$WIFI_INTERFACE.pid ] && kill `cat /var/run/udhcpc.$WIFI_INTERFACE.pid` + ifconfig $WIFI_INTERFACE down + iwconfig $WIFI_INTERFACE txpower auto + /etc/init.d/network.sh restart + # Remove tmp file (could be use to have wireless profiles) + rm -f /tmp/wifi.conf +} + +# We must spleep 4 sec to refresh networks list. +stop_wifi_connexion() +{ + sed -i s/`grep ^WIFI= /etc/network.conf`/WIFI=\"no\"/ \ + /etc/network.conf + [ -x /etc/init.d/wpa_supplicant ] && /etc/init.d/wpa_supplicant stop + ifconfig $WIFI_INTERFACE down + iwconfig $WIFI_INTERFACE txpower off + [ -s /var/run/udhcpc.$WIFI_INTERFACE.pid ] && kill `cat /var/run/udhcpc.$WIFI_INTERFACE.pid` + sleep 4 +} + +# Favorites wireless networks use only 3 values: essid. key and type of +# key +FAVORITES_WIFI_list() +{ + for i in $FAVORITES_WIFI/*.conf + do + WIFI_ESSID="" + WIFI_KEY="" + WIFI_KEY_TYPE="" + . "$i" + [ -z "$WIFI_ESSID" ] && WIFI_ESSID="Bad config file: $i" + [ -z "$WIFI_KEY_TYPE" ] && WIFI_KEY_TYPE="-" + if [ -n "$WIFI_KEY" ]; then + WIFI_KEY="********" + else + WIFI_KEY="-" + fi + echo "$WIFI_ESSID | $WIFI_KEY_TYPE | $WIFI_KEY" + done +} + +favorite_wifi_actions() +{ + cp -a $FAVORITES_WIFI/"$FAVORITE".conf /tmp/wifi.conf + . /tmp/wifi.conf + export CONNECT_FAVORITE=" + + + + + + + + + + + + + + + +" + gtkdialog --center --program=CONNECT_FAVORITE >/dev/null +} + +add_favorite_network_box() +{ + ADD_FAVORITE=' + + + + + + + + + + + WIFI_ESSID + + + + + + + + WIFI_KEY + + + + + + + ' + tmp="${ADD_FAVORITE}$WIFI_KEY_TYPE" + for i in none WEP WPA any; do + tmp=${tmp}"$i" + done + export ADD_FAVORITE=${tmp}' + WIFI_KEY_TYPE + + + + + + + +' + gtkdialog --center --program=ADD_FAVORITE #>/dev/null +} + +# GUI functions + +helpbutton() +{ + local label; + label="" + [ -n "$3" ] || label="" + cat << EOT + +EOT +} + +manbutton() +{ + cat << EOT + +EOT +} + +# Independant dialog to connect on a wireless network. If encryption +# is on we ask for the security key. +connect_to_essid() +{ + SCAN=`iwlist $WIFI_INTERFACE scanning essid "$ESSID_LIST" | grep -A 6 "ESSID"` + WIFI_ESSID="$ESSID_LIST" + ENCRYPTION=`echo $SCAN | sed 's/.*key:\([^ ]*\).*/\1/'` + # Create tmp file used by active_wifi_connexion() + cat > /tmp/wifi.conf << _EOF_ +# Wireless connexion configuration file. +WIFI_ESSID="$ESSID_LIST" +_EOF_ + CONNECT_ESSID=" + + + + + " + # We mybe need a key to connect + if [ "$ENCRYPTION" = "on" ] && [ "$ESSID_LIST" != "any" ]; then + # WPA + if echo "$SCAN" | grep -q WPA; then + echo 'WIFI_KEY_TYPE="WPA"' >> /tmp/wifi.conf + CONNECT_ESSID=${CONNECT_ESSID}' + + + + + + . /etc/network.conf; echo "$WIFI_KEY" + WIFI_KEY + + ' + else + # WEP + echo 'WIFI_KEY_TYPE="WEP"' >> /tmp/wifi.conf + CONNECT_ESSID=${CONNECT_ESSID}' + + + + + + . /etc/network.conf; echo "$WIFI_KEY" + WIFI_KEY + + ' + fi + else + # No encryption + echo 'WIFI_KEY=""' >> /tmp/wifi.conf + echo 'WIFI_KEY_TYPE=""' >> /tmp/wifi.conf + start_wifi_connection + exit 0 + fi + # Add key to config file so active_wifi_connexion() can use it. + # WIFI_KEY is not exported if we quote with --> " + export CONNECT_ESSID=${CONNECT_ESSID}' + + + + + +' + gtkdialog --center --program=CONNECT_ESSID #>/dev/null +} + +# Wifibox start with Networks tab. +box() +{ + WIFI_DIALOG=" + + + + + + + + 500160 + ESSID_LIST + + $0 detect_wifi_networks + any | * | off | (auto-connect) + $0 connect_to_essid + refresh:ESSID_LIST + refresh:WIFI_ESSID + refresh:WIFI_KEY + refresh:WIFI_KEY_TYPE + + + + + + + + " + + # Favorites networks + WIFI_DIALOG=${WIFI_DIALOG}" + + + 500160 + FAVORITE + + $0 FAVORITES_WIFI_list + any | - | - + $0 favorite_wifi_actions + refresh:FAVORITE + refresh:ESSID_LIST + refresh:WIFI_ESSID + refresh:WIFI_KEY + refresh:WIFI_KEY_TYPE + + + + + + + + " + + # Configuration tab + WIFI_DIALOG=${WIFI_DIALOG}' + + + + + + + + . /etc/network.conf; echo "$WIFI_INTERFACE" + WIFI_INTERFACE + + + + + + + + . /etc/network.conf; echo "$WIFI_ESSID" + WIFI_ESSID + + + + + + + + . /etc/network.conf; echo "$WIFI_KEY" + WIFI_KEY + + ' + tmp2="${WIFI_DIALOG}$WIFI_KEY_TYPE" + for i in none WEP WPA any; do + [ "$i" = "$WIFI_KEY_TYPE" ] || tmp2="$tmp2$i" + done + tmp3=' WIFI_KEY_TYPE + + + + + + + + + + . /etc/network.conf; echo "$WIFI_CHANNEL" + WIFI_CHANNEL + + + + WIFI_MODE' + tmp2="$tmp2$tmp3$WIFI_MODE" + for i in managed ad-hoc master repeater secondary monitor; do + [ "$i" = "$WIFI_MODE" ] || tmp2="$tmp2$i" + done + tmp3=' + + + + + + + . /etc/network.conf; echo "$WIFI_IWCONFIG_ARGS" + WIFI_IWCONFIG_ARGS + ' + WIFI_DIALOG="$tmp$tmp2$tmp3 + $(helpbutton iwconfig 80x24) + $(manbutton 8 iwconfig) + + " + + # Start Button for manual configuration. + WIFI_DIALOG=${WIFI_DIALOG}' + + + + ' + + # Kernel Modules, firmware and tazndisbox note + button. + WIFI_DIALOG=${WIFI_DIALOG}" + + + + + + + + + + + " + # Display firmware stuff, tazndisbox button if installed and close + # tab + notebook + if [ -x /usr/bin/tazndisbox ]; then + WIFI_DIALOG=${WIFI_DIALOG}" + " + fi + WIFI_DIALOG=${WIFI_DIALOG}" + + + + + + + MODULE" + WIFI_DIALOG="${WIFI_DIALOG}$(find /lib/modules/$(uname -r)/kernel/drivers/net/wireless -type f 2> /dev/null | sed 's,/.*/\(.*\).ko.*,\1,')" + WIFI_DIALOG=${WIFI_DIALOG}' + + + + + ' + # Firmware stuff. + tmp=$(for i in /usr/bin/get*firmware; do + [ -x $i ] || continue + [ "$i" = "/usr/bin/get-wifi-firmware" ] && continue + [ -d /var/lib/tazpkg/installed/${i#/usr/bin/get-} ] && continue + echo "${i#/usr/bin/get-}"; done) + [ -n "$tmp" ] && tmp=" + + + + + FIRMWARE$tmp + + + " + +# Bottom buttons +export WIFI_DIALOG=${WIFI_DIALOG}${tmp}" + + + + + + + + +" + gtkdialog --center --program=WIFI_DIALOG >/dev/null 2>&1 +} + +if [ -n "$1" ]; then + $1 +else + box +fi + +exit 0