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

Current state, features stabilized and open for bugfixes and translations.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Sep 09 03:27:04 2013 +0300 (2013-09-09)
parents cc27e2f27556
children c79e656b37a5
line source
1 #!/bin/sh
2 #
3 # Small Wi-Fi utility to quickly connect to a network. Easy network connection
4 # is most important, this tool provides a quick way to connect or change Wi-Fi
5 # settings while full network configuration is done in TazPanel.
6 #
7 # Copyright (C) 2012-2013 SliTaz GNU/Linux - GNU GPL v2
8 #
9 # Authors: Christophe Lincoln <pankso@slitaz.org>
10 #
11 . /lib/libtaz.sh
12 export TEXTDOMAIN='slitaz-boxes' #i18n
14 usage() {
15 newline; _ 'Small Wi-Fi utility to quickly connect to a network.'
16 newline; boldify "$(_ 'Usage:')"
17 echo " $(basename $0) [$(_ 'interface')]"
18 newline
19 }
21 # Start a Wi-Fi connection
22 start_wifi() {
23 sed -i \
24 -e s'/^DHCP=.*/DHCP="yes"/' \
25 -e s'/^STATIC=.*/STATIC="no"/' \
26 -e s'/^WIFI=.*/WIFI="yes"/' \
27 /etc/network.conf
28 ifconfig $WIFI_INTERFACE up
29 iwconfig $WIFI_INTERFACE txpower auto
30 /etc/init.d/network.sh start
31 }
33 # Catch ESSIDs and format output for GTK tree. We get the list of
34 # networks by Cell and without spaces.
35 detect_wifi() {
36 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
37 ifconfig $WIFI_INTERFACE up
38 echo -e "$( _n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
39 for i in $(iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}')
40 do
41 scan=$(iwlist $WIFI_INTERFACE scan last | \
42 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
43 sed s/"Cell "/Cell-/ | grep -A 5 "$i")
44 essid=$(echo $scan | cut -d '"' -f 2)
46 if echo "$scan" | grep -q Quality; then
47 quality=$(echo $scan | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/')
48 else
49 quality="$(_n '-')"
50 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
58 # Connected or not connected...
59 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
60 grep -q inet && iwconfig $WIFI_INTERFACE | \
61 grep ESSID | grep -q -w "$essid"; then
62 status=connected
63 else
64 status="$(_n '-')"
65 fi
67 echo -e "$essid\n$quality\n$cryto\n$status"
68 done
69 fi
70 }
72 # Prompt for password or connect
73 connect_main() {
74 case $keytype in
75 WPA) label="$(_n 'WPA Password:')" ;;
76 WEP) label="$(_n 'WEP Password:')" ;;
77 *) label= ;;
78 esac
79 case $keytype in
80 WPA|WEP)
81 icon=network-wireless
82 yad --title="$(_n 'Wi-Fi connection')" --window-icon=$icon \
83 --width=520 --height=140 --on-top --center \
84 --image=$icon --image-on-top \
85 --text="$(_n 'Connection to:') <b>$essid</b>" \
86 --form \
87 --field="$label:H" ;;
88 none) continue ;;
89 *) exit 0 ;;
90 esac
91 }
93 connect() {
94 main=$(connect_main)
95 ret=$?
96 # Deal with --button values
97 case $ret in
98 1) exit 0 ;;
99 *) continue ;;
100 esac
101 /etc/init.d/network.sh stop
102 sleep 1
103 key=$(echo "$main" | cut -d '|' -f 1)
104 sed -i \
105 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
106 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
107 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$keytype\"/" \
108 /etc/network.conf
109 start_wifi
110 }
112 # Main GUI box function with pure Yad spec
113 wifi_main() {
114 icon=network-wireless
115 detect_wifi | yad --title="$(_n 'Wi-Fi network')" --window-icon=$icon \
116 --width=520 --height=300 --on-top --center \
117 --image=$icon --image-on-top \
118 --text="$(_n '<b>Connect to a Wi-Fi network</b> (Double click to connect)')" \
119 --list \
120 --column "$(_n 'ESSID Name')" --column "$(_n 'Quality')" \
121 --column "$(_n 'Encryption')" --column "$(_n 'Status')" \
122 --button="$(_n 'Start Wi-Fi'):4" --button="$(_n 'Stop Wi-Fi'):3" \
123 --button="gtk-preferences:2" --button="gtk-close:1"
124 }
126 # Main function
127 wifi() {
128 # Store box results
129 main=$(wifi_main)
130 ret=$?
131 # Deal with --button values
132 case $ret in
133 1) exit 0 ;;
134 2) tazweb http://tazpanel:82/network.cgi?wifi && exit 0 ;;
135 3) /etc/init.d/network.sh stop && exit 0 ;;
136 3) start_wifi && exit 0 ;;
137 *) continue ;;
138 esac
139 if [ -n "$main" ]; then
140 essid=$(echo "$main" | cut -d "|" -f 1)
141 keytype=$(echo "$main" | cut -d "|" -f 3)
142 connect
143 fi
144 }
146 #
147 # Script commands
148 #
150 case "$1" in
151 -h|--help|usage|help|-*)
152 usage ;;
153 *)
154 # Only for root.
155 if [ $(id -u) != 0 ]; then
156 exec tazbox su $0 $@
157 exit 0
158 fi
160 . /etc/network.conf
161 [ -n "$1" ] && WIFI_INTERFACE="$1"
162 wifi ;;
163 esac
165 exit 0