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

tazbox: fix subox icon when ~/.local/share/applications is absent; fix working with freegeoip; write full list of icons used; all other files: 2015 and insert blank lines.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Apr 17 07:35:02 2015 +0300 (2015-04-17)
parents 720dfc98b497
children 0f57e55885af
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-2015 SliTaz GNU/Linux - GNU GPL v2
8 #
9 # Authors: Christophe Lincoln <pankso@slitaz.org>
10 #
12 . /lib/libtaz.sh
13 export TEXTDOMAIN='slitaz-boxes' #i18n
15 usage() {
16 newline; _ 'Small Wi-Fi utility to quickly connect to a network.'
17 newline; boldify "$(_ 'Usage:')"
18 echo " $(basename $0) [$(_ 'interface')]"
19 newline
20 }
23 # Start a Wi-Fi connection
25 start_wifi() {
26 sed -i \
27 -e s'/^DHCP=.*/DHCP="yes"/' \
28 -e s'/^STATIC=.*/STATIC="no"/' \
29 -e s'/^WIFI=.*/WIFI="yes"/' \
30 /etc/network.conf
31 ifconfig $WIFI_INTERFACE up
32 iwconfig $WIFI_INTERFACE txpower auto
33 /etc/init.d/network.sh start
34 }
37 # Catch ESSIDs and format output for GTK tree. We get the list of
38 # networks by Cell and without spaces.
40 detect_wifi() {
41 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
42 ifconfig $WIFI_INTERFACE up
43 echo -e "$( _n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
44 for i in $(iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}')
45 do
46 scan=$(iwlist $WIFI_INTERFACE scan last | \
47 awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
48 sed s/"Cell "/Cell-/ | grep -A 5 "$i")
49 essid=$(echo $scan | cut -d '"' -f 2)
51 if echo "$scan" | grep -q Quality; then
52 quality=$(echo $scan | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/')
53 else
54 quality="$(_n '-')"
55 fi
57 cryto=$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')
58 # Check encryption type
59 if echo "$scan" | grep -q WPA*; then
60 cryto="WPA"
61 fi
63 # Connected or not connected...
64 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
65 grep -q inet && iwconfig $WIFI_INTERFACE | \
66 grep ESSID | grep -q -w "$essid"; then
67 status=connected
68 else
69 status="$(_n '-')"
70 fi
72 echo -e "$essid\n$quality\n$cryto\n$status"
73 done
74 fi
75 }
78 # Prompt for password or connect
80 connect_main() {
81 case $keytype in
82 WPA) label="$(_n 'WPA Password:')" ;;
83 WEP) label="$(_n 'WEP Password:')" ;;
84 *) label= ;;
85 esac
86 case $keytype in
87 WPA|WEP)
88 icon='network-wireless'
89 yad --title="$(_n 'Wi-Fi connection')" --window-icon=$icon \
90 --width=520 --height=140 --on-top --center \
91 --image=$icon --image-on-top \
92 --text="$(_n 'Connection to:') <b>$essid</b>" \
93 --form \
94 --field="$label:H" ;;
95 none) continue ;;
96 *) exit 0 ;;
97 esac
98 }
101 connect() {
102 main=$(connect_main)
103 ret=$?
104 # Deal with --button values
105 case $ret in
106 1) exit 0 ;;
107 *) continue ;;
108 esac
109 /etc/init.d/network.sh stop
110 sleep 1
111 key=$(echo "$main" | cut -d '|' -f 1)
112 sed -i \
113 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
114 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
115 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$keytype\"/" \
116 /etc/network.conf
117 start_wifi
118 }
121 # Main GUI box function with pure Yad spec
123 wifi_main() {
124 icon='network-wireless'
125 detect_wifi | yad --title="$(_n 'Wi-Fi network')" --window-icon=$icon \
126 --width=520 --height=300 --on-top --center \
127 --image=$icon --image-on-top \
128 --text="$(_n '<b>Connect to a Wi-Fi network</b> (Double click to connect)')" \
129 --list \
130 --column "$(_n 'ESSID Name')" --column "$(_n 'Quality')" \
131 --column "$(_n 'Encryption')" --column "$(_n 'Status')" \
132 --button="$(_n 'Start Wi-Fi'):4" --button="$(_n 'Stop Wi-Fi'):3" \
133 --button="gtk-preferences:2" --button="gtk-close:1"
134 }
137 # Main function
139 wifi() {
140 # Store box results
141 main=$(wifi_main)
142 ret=$?
143 # Deal with --button values
144 case $ret in
145 1) exit 0 ;;
146 2) tazweb --notoolbar http://tazpanel:82/network.cgi?wifi && exit 0 ;;
147 3) /etc/init.d/network.sh stop && exit 0 ;;
148 3) start_wifi && exit 0 ;;
149 *) continue ;;
150 esac
151 if [ -n "$main" ]; then
152 essid=$(echo "$main" | cut -d "|" -f 1)
153 keytype=$(echo "$main" | cut -d "|" -f 3)
154 connect
155 fi
156 }
159 #
160 # Script commands
161 #
163 case "$1" in
164 -h|--help|usage|help|-*)
165 usage ;;
166 *)
167 # Only for root.
168 if [ $(id -u) != 0 ]; then
169 exec tazbox su $0 $@
170 exit 0
171 fi
173 . /etc/network.conf
174 [ -n "$1" ] && WIFI_INTERFACE="$1"
175 wifi ;;
176 esac
178 exit 0