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

tazbox video: allow window resize
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu May 26 13:51:02 2016 +0200 (2016-05-26)
parents 49dd8ce35bd4
children
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-2016 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 crypto=$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')
58 # Check encryption type
59 if echo "$scan" | grep -q WPA*; then
60 crypto="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="$(_n 'connected')"
68 else
69 status="$(_n '-')"
70 fi
72 echo -e "$essid\n$quality\n${crypto/off/NONE}\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 if iwconfig 2>/dev/null | grep -q 'Tx-Power=off'; then
126 startstop="$(_n 'Start Wi-Fi')!media-playback-start:4"
127 refresh=''
128 else
129 startstop="$(_n 'Stop Wi-Fi')!media-playback-stop:3"
130 refresh="--button=gtk-refresh:5"
131 fi
132 detect_wifi | yad --title="$(_n 'Wi-Fi network')" --window-icon=$icon \
133 --width=520 --height=300 --center \
134 --image=$icon --image-on-top \
135 --text="$(_n '<b>Connect to a Wi-Fi network</b> (Double click to connect)')" \
136 --list \
137 --column "$(_n 'ESSID Name')" --column "$(_n 'Quality')" \
138 --column "$(_n 'Encryption')" --column "$(_n 'Status')" \
139 --button="$startstop" $refresh \
140 --button="gtk-preferences:2" --button="gtk-close:1"
141 }
144 # Main function
146 wifi() {
147 # Store box results
148 main=$(wifi_main)
149 ret=$?
150 # Deal with --button values
151 case $ret in
152 1) exit 0 ;;
153 2) tazpanel network#wifi; exit 0 ;;
154 3) /etc/init.d/network.sh stop; $0 ;;
155 4) start_wifi; $0 ;;
156 5) $0 ;;
157 esac
158 if [ -n "$main" ]; then
159 essid=$(echo "$main" | cut -d "|" -f 1)
160 keytype=$(echo "$main" | cut -d "|" -f 3)
161 connect
162 fi
163 }
166 #
167 # Script commands
168 #
170 case "$1" in
171 -h|--help|usage|help|-*)
172 usage ;;
173 *)
174 # Only for root.
175 if [ $(id -u) -ne 0 ]; then
176 exec tazbox su $0 $@
177 exit 0
178 fi
180 . /etc/network.conf
181 [ -n "$1" ] && WIFI_INTERFACE="$1"
182 wifi ;;
183 esac
185 exit 0