slitaz-tools view boxes/wifi-box @ rev 745

Rename wifibox to wifi-box (we may have a tools-gtkdialog packages with old boxes, so avoid name conflicts)
author Christophe Lincoln <pankso@slitaz.org>
date Sun Apr 29 23:07:19 2012 +0200 (2012-04-29)
parents boxes/wifibox@96a05e0a165d
children e232f0e1413a
line source
1 #!/bin/sh
2 #
3 # Small Wifi utility to quickly connect to a network. Easy network connection is
4 # most important, this tool provides a quick way to connect or change wifi
5 # settings while full network configuration is done in TazPanel.
6 #
7 # Copyright (C) 2012 SliTaz GNU/Linux - GNU gpl v2
8 #
9 # Authors : Christophe Lincoln <pankso@slitaz.org>
10 #
12 # Only for root.
13 if [ $(id -u) != 0 ]; then
14 exec tazbox su $(basename $0) $@
15 exit 0
16 fi
18 # Internationalization
19 . /usr/bin/gettext.sh
20 TEXTDOMAIN='slitaz-boxes'
21 export TEXTDOMAIN
23 # Start a wifi connection
24 start_wifi() {
25 sed -i \
26 -e s'/^DHCP=.*/DHCP="yes"/' \
27 -e s'/^STATIC=.*/STATIC="no"/' \
28 -e s'/^WIFI=.*/WIFI="yes"/' \
29 /etc/network.conf
30 ifconfig $WIFI_INTERFACE up
31 iwconfig $WIFI_INTERFACE txpower auto
32 /etc/init.d/network.sh start
33 }
35 # Catch essids and format output for GTK tree. We get the list of
36 # networks by Cell and without spaces.
37 detect_wifi() {
38 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
39 ifconfig $WIFI_INTERFACE up
40 echo -e "any\nN/A\nnone\n-"
41 for i in $(iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}')
42 do
43 scan=$(iwlist $WIFI_INTERFACE scan last | \
44 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
45 sed s/"Cell "/Cell-/ | grep -A 5 "$i")
46 essid=$(echo $scan | cut -d '"' -f 2)
47 if echo "$scan" | grep -q Quality; then
48 quality=$(echo $scan | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/')
49 else
50 quality="-"
51 fi
52 cryto=$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')
53 # Check encryption type
54 if echo "$scan" | grep -q WPA*; then
55 cryto="WPA"
56 fi
57 # Connected or not connected...
58 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
59 grep -q inet && iwconfig $WIFI_INTERFACE | \
60 grep ESSID | grep -q -w "$essid"; then
61 status=connected
62 else
63 status="-"
64 fi
66 echo -e "$essid\n$quality\n$cryto\n$status"
67 done
68 fi
69 }
71 # Prompt for password or connect
72 connect_main() {
73 case $keytype in
74 WPA|WEP)
75 title=$(gettext "Wifi connection")
76 text=$(gettext "Connection to:")
77 yad --form --width=520 --height=140 \
78 --center --on-top --window-icon="network-wireless" \
79 --image="network-wireless" --image-on-top \
80 --title="$title" --text="$text <b>$essid</b>" \
81 --field="$keytype $(gettext "Password:"):H" ;;
82 none) continue ;;
83 *) exit 0 ;;
84 esac
85 }
87 connect() {
88 main=$(connect_main)
89 ret=$?
90 # Deal with --button values
91 case $ret in
92 1) exit 0 ;;
93 *) continue ;;
94 esac
95 /etc/init.d/network.sh stop
96 sleep 1
97 key=$(echo "$main" | cut -d '|' -f 1)
98 sed -i \
99 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
100 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
101 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$keytype\"/" \
102 /etc/network.conf
103 start_wifi
104 }
106 # Main GUI box function with pure Yad spec
107 wifi_main() {
108 title=$(gettext "Wifi network")
109 text=$(gettext "<b>Connect to a wifi network</b> \(Double click to connect\)")
110 detect_wifi | yad --list --width=520 --height=300 \
111 --center --on-top --window-icon="network-wireless" \
112 --image="network-wireless" --image-on-top \
113 --title="$title" --text="$text" \
114 --column "$(gettext "ESSID Name")" --column "$(gettext "Quality")" \
115 --column "$(gettext "Encryption")" --column "$(gettext "Status")" \
116 --button="Start wifi:4" --button="Stop wifi:3" \
117 --button="Configuration:2" --button="gtk-close:1"
118 }
120 # Main function
121 wifi() {
122 # Store box results
123 main=$(wifi_main)
124 ret=$?
125 # Deal with --button values
126 case $ret in
127 1) exit 0 ;;
128 2) tazweb http://tazpanel:82/network.cgi?wifi && exit 0 ;;
129 3) /etc/init.d/network.sh stop && exit 0 ;;
130 3) start_wifi && exit 0 ;;
131 *) continue ;;
132 esac
133 if [ -n "$main" ]; then
134 essid=$(echo "$main" | cut -d "|" -f 1)
135 keytype=$(echo "$main" | cut -d "|" -f 3)
136 connect
137 fi
138 }
140 #
141 # Script commands
142 #
144 case "$1" in
145 usage|help|-*)
146 echo "$(gettext "Usage:") $(basename $0) [interface]" ;;
147 *)
148 . /etc/network.conf
149 [ -n "$1" ] && WIFI_INTERFACE="$1"
150 wifi ;;
151 esac
153 exit 0