tazpanel view network.cgi @ rev 321

network.cgi: fix scan parsing, use any key type
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jun 30 14:36:32 2012 +0200 (2012-06-30)
parents 42367a53cf50
children 1602cb73097e
line source
1 #!/bin/sh
2 #
3 # Network configuration CGI interface
4 #
5 # Copyright (C) 2012 SliTaz GNU/Linux - BSD License
6 #
8 # Common functions from libtazpanel
9 . lib/libtazpanel
10 get_config
11 header
13 TITLE=$(gettext 'TazPanel - Network')
15 # Catch ESSIDs and format output for GTK tree. We get the list of
16 # networks by Cell and without spaces.
17 detect_wifi_networks()
18 {
19 cat << EOT
20 <table class="zebra outbox">
21 <thead>
22 <tr>
23 <td>$(gettext 'Name')</td>
24 <td>$(gettext 'Quality')</td>
25 <td>$(gettext 'Encryption')</td>
26 <td>$(gettext 'Status')</td>
27 </tr>
28 </thead>
29 <tbody>
30 EOT
31 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
32 ifconfig $WIFI_INTERFACE up
33 for i in $(iwlist $WIFI_INTERFACE scan | sed '/Cell /!d;s/.*Cell \([^ ]*\).*/Cell.\1/')
34 do
35 SCAN=$(iwlist $WIFI_INTERFACE scan last | sed "/$i/,/Cell/!d" | sed '$d')
36 ESSID=$(echo $SCAN | sed '/ESSID/!d;s/.*ESSID:"\(.*\)"$/\1/')
37 if echo "$SCAN" | grep -q Quality; then
38 QUALITY=$(echo $SCAN | sed '/Quality/!d;s/.*Quality:\([^ ]*\).*/\1/')
39 else
40 QUALITY="-"
41 fi
42 ENCRYPTION=$(echo $SCAN | sed '/key:/!d;s/.*key:\([^ ]*\).*/\1/')
43 # Check encryption type
44 if echo "$SCAN" | grep -q WPA*; then
45 ENCRYPTION="WPA"
46 fi
47 # Connected or not connected...
48 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
49 fgrep -q inet && iwconfig $WIFI_INTERFACE | \
50 grep ESSID | fgrep -q -w "$ESSID"; then
51 status=$(gettext 'Connected')
52 else
53 status="---"
54 fi
55 echo '<tr>'
56 echo "<td><a href=\"$SCRIPT_NAME?wifi&select=$ESSID&keytype=$ENCRYPTION\">
57 <img src='$IMAGES/wireless.png' />$ESSID</a></td>"
58 echo "<td>$QUALITY</td><td>$ENCRYPTION</td><td>$status $ip</td>"
59 echo '</tr>'
60 done
61 fi
62 cat << EOT
63 </tbody>
64 </table>
65 EOT
66 }
68 # Start a wifi connection
69 start_wifi() {
70 sed -i \
71 -e s'/^DHCP=.*/DHCP="yes"/' \
72 -e s'/^WIFI=.*/WIFI="yes"/' \
73 -e s'/^STATIC=.*/STATIC="no"/' /etc/network.conf
74 ifconfig $WIFI_INTERFACE up
75 iwconfig $WIFI_INTERFACE txpower auto
76 /etc/init.d/network.sh restart | log
77 sleep 2
78 }
80 # Actions commands before page is displayed
81 case " $(GET) " in
82 *\ start\ *)
83 # Here we sleep a bit to let udhcp get the lease before reloading
84 # the page with status
85 /etc/init.d/network.sh start | log
86 sleep 2 ;;
87 *\ stop\ *)
88 /etc/init.d/network.sh stop | log ;;
89 *\ restart\ *)
90 /etc/init.d/network.sh restart | log ;;
91 *\ start-wifi\ *) start_wifi ;;
92 *\ hostname\ *)
93 get_hostname="$(GET hostname)"
94 echo $(eval_gettext 'Changed hostname: $get_hostname') | log
95 echo "$get_hostname" > /etc/hostname ;;
96 esac
98 # Get values only now since they could have been modified by actions.
99 . /etc/network.conf
101 #
102 # Main Commands for pages
103 #
105 case " $(GET) " in
106 *\ scan\ *)
107 # Scan open ports
108 scan=$(GET scan)
109 xhtml_header
110 LOADING_MSG=$(gettext 'Scanning open ports...')
111 loading_msg
112 cat << EOT
113 <h2>$(eval_gettext 'Port scanning for $scan')</h2>
115 <pre>$(pscan -b $scan)</pre>
116 EOT
117 ;;
119 *\ eth\ *)
120 # Wired connections settings
121 xhtml_header
122 if [ "$(GET ip)" ]; then
123 DHCP=no
124 STATIC=no
125 [ -n "$(GET dhcp)" ] && DHCP=yes
126 [ -n "$(GET static)" ] && STATIC=yes
127 LOADING_MSG=$(gettext 'Setting up IP...')
128 loading_msg
129 sed -i \
130 -e s"/^INTERFACE=.*/INTERFACE=\"$(GET iface)\""/ \
131 -e s"/^DHCP=.*/DHCP=\"$DHCP\"/" \
132 -e s"/^STATIC=.*/STATIC=\"$STATIC\"/" \
133 -e s"/^NETMASK=.*/NETMASK=\"$(GET netmask)\"/" \
134 -e s"/^GATEWAY=.*/GATEWAY=\"$(GET gateway)\"/" \
135 -e s"/^DNS_SERVER=.*/DNS_SERVER=\"$(GET dns)\"/" \
136 -e s"/^IP=.*/IP=\"$(GET ip)\"/" /etc/network.conf
137 /etc/init.d/network stop | log
138 sleep 2
139 /etc/init.d/network start | log
140 . /etc/network.conf
141 fi
142 cat << EOT
143 <h2>$(gettext 'Ethernet connection')</h2>
145 <p>$(gettext "Here you can configure a wired connection using DHCP to \
146 automatically get a random IP or configure a static/fixed IP")</p>
148 <section>
149 <h3>$(gettext 'Configuration')</h3>
150 <form method="get" action="$SCRIPT_NAME">
151 <input type="hidden" name="eth" />
152 <table>
153 <thead>
154 <tr>
155 <td>$(gettext 'Name')</td>
156 <td>$(gettext 'Value')</td>
157 </tr>
158 </thead>
159 <tbody>
160 <tr>
161 <td>$(gettext 'Interface')</td>
162 <td><input type="text" name="iface" size="20" value="$INTERFACE" /></td>
163 </tr>
164 <tr>
165 <td>$(gettext 'IP address')</td>
166 <td><input type="text" name="ip" size="20" value="$IP" /></td>
167 </tr>
168 <tr>
169 <td>$(gettext 'Netmask')</td>
170 <td><input type="text" name="netmask" size="20" value="$NETMASK" /></td>
171 </tr>
172 <tr>
173 <td>$(gettext 'Gateway')</td>
174 <td><input type="text" name="gateway" size="20" value="$GATEWAY" /></td>
175 </tr>
176 <tr>
177 <td>$(gettext 'DNS server')</td>
178 <td><input type="text" name="dns" size="20" value="$DNS_SERVER" /></td>
179 </tr>
180 </tbody>
181 </table>
182 <input type="submit" name="static" value="$(gettext 'Activate (static)')">
183 <input type="submit" name="dhcp" value="$(gettext 'Activate (DHCP)')">
184 <input type="submit" name="disable" value="$(gettext 'Disable')">
185 </form>
186 </section>
188 <section>
189 <h3>$(gettext 'Configuration file')</h3>
191 <p>$(gettext "These values are the ethernet settings in the main \
192 /etc/network.conf configuration file")</p>
193 <pre>
194 $(grep ^[A-V] /etc/network.conf | syntax_highlighter conf)
195 </pre>
196 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
197 <img src="$IMAGES/edit.png" />$(gettext 'Manual Edit')</a>
198 </section>
199 EOT
200 ;;
201 *\ wifi\ *)
202 # Wireless connections settings
203 xhtml_header
204 LOADING_MSG=$(gettext 'Scanning wireless interface...')
205 loading_msg
206 . /etc/network.conf
207 cat << EOT
208 <h2>$(gettext 'Wireless connection')</h2>
209 <div id="actions">
210 <a class="button" href="$SCRIPT_NAME?wifi&start-wifi=start-wifi">
211 <img src="$IMAGES/start.png" />$(gettext 'Start')</a>
212 <a class="button" href="$SCRIPT_NAME?wifi&stop=stop">
213 <img src="$IMAGES/stop.png" />$(gettext 'Stop')</a>
214 <a class="button" href="$SCRIPT_NAME?wifi=scan">
215 <img src="$IMAGES/recharge.png" />$(gettext 'Scan')</a>
216 </div>
217 $(detect_wifi_networks)
218 EOT
219 WIFI_KEY="$(GET key)"
220 case "$(GET keytype)" in
221 ''|off) WIFI_KEY_TYPE=none ;;
222 * WIFI_KEY_TYPE=any ;;
223 esac
224 if [ "$(GET essid)" ]; then
225 /etc/init.d/network.sh stop | log
226 sed -i \
227 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$(GET essid)\""/ \
228 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$WIFI_KEY\"/" \
229 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$WIFI_KEY_TYPE\"/" \
230 /etc/network.conf
231 . /etc/network.conf
232 start_wifi
233 fi
234 # ESSID names are clickable
235 if [ "$(GET select)" ]; then
236 if [ "$(GET select)" != "$WIFI_ESSID" ]; then
237 WIFI_KEY=""
238 fi
239 WIFI_ESSID="$(GET select)"
240 fi
241 cat << EOT
242 <section>
243 <h3>$(gettext 'Connection')</h3>
244 <form method="get" action="$SCRIPT_NAME">
245 <input type="hidden" name="connect-wifi" />
246 $(table_start)
247 <thead>
248 <tr>
249 <td>$(gettext 'Name')</td>
250 <td>$(gettext 'Value')</td>
251 </tr>
252 </thead>
253 <tr>
254 <td>$(gettext 'Wifi name (ESSID)')</td>
255 <td><input type="text" name="essid" size="30" value="$WIFI_ESSID" /></td>
256 </tr>
257 <tr>
258 <td>$(gettext 'Password (Wifi key)')</td>
259 <td><input type="password" name="key" size="30" value="$WIFI_KEY" /></td>
260 </tr>
261 <tr>
262 <td>$(gettext 'Encryption type')</td>
263 <td><input type="text" name="keytype" size="30" value="$WIFI_KEY_TYPE" /></td>
264 </tr>
265 $(table_end)
266 <input type="submit" name="wifi" value="$(gettext 'Configure')" />
267 </form>
268 </section>
270 <section>
271 <h3>$(gettext 'Configuration file')</h3>
273 <p>$(gettext "These values are the wifi settings in the main /etc/network.conf \
274 configuration file")</p>
276 <pre>$(grep ^WIFI /etc/network.conf | syntax_highlighter conf)</pre>
278 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
279 <img src="$IMAGES/edit.png" />$(gettext 'Manual Edit')</a>
280 </section>
282 <section>
283 <h3>$(gettext 'Output of iwconfig')</h3>
285 <pre>$(iwconfig)</pre>
286 </section>
287 EOT
288 ;;
289 *)
290 # Main Network page starting with a summary
291 xhtml_header
292 hostname=$(cat /etc/hostname)
293 cat << EOT
294 <h2>$(gettext 'Networking')</h2>
296 <p>$(gettext 'Manage network connections and services')</p>
298 <section>
299 <div id="actions">
300 <div class="float-left">
301 <a class="button" href="$SCRIPT_NAME?start">
302 <img src="$IMAGES/start.png" />$(gettext 'Start')</a>
303 <a class="button" href="$SCRIPT_NAME?stop">
304 <img src="$IMAGES/stop.png" />$(gettext 'Stop')</a>
305 <a class="button" href="$SCRIPT_NAME?restart">
306 <img src="$IMAGES/recharge.png" />$(gettext 'Restart')</a>
307 </div>
308 <div class="float-right">
309 $(gettext 'Configuration:')
310 <a class="button" href="index.cgi?file=/etc/network.conf">network.conf</a>
311 <a class="button" href="$SCRIPT_NAME?eth">Ethernet</a>
312 <a class="button" href="$SCRIPT_NAME?wifi">Wireless</a>
313 </div>
314 </div>
316 $(list_network_interfaces)
317 </section>
319 <section>
320 <h3 id="hosts">$(gettext 'Hosts')</h3>
322 <pre>$(cat /etc/hosts)</pre>
324 <a class="button" href="index.cgi?file=/etc/hosts&action=edit">
325 <img src="$IMAGES/edit.png" />$(gettext 'Edit hosts')</a>
326 </section>
328 <section>
329 <h3>$(gettext 'Hostname')</h3>
331 <form method="get" name="$SCRIPT_NAME">
332 <input type="text" name="hostname" value="$hostname" />
333 <input type="submit" value="$(gettext 'Change hostname')" />
334 </form>
335 </section>
337 <section>
338 <h3 id="ifconfig">$(gettext 'Output of ifconfig')</h3>
340 <pre>$(ifconfig)</pre>
341 </section>
343 <section>
344 <h3 id="routing">$(gettext 'Routing table')</h3>
346 <pre>$(route -n)</pre>
347 </section>
349 <section>
350 <h3 id="dns">$(gettext 'Domain name resolution')</h3>
352 <pre>$(cat /etc/resolv.conf)</pre>
353 </section>
355 <section>
356 <h3 id="arp">$(gettext 'ARP table')</h3>
358 <pre>$(arp)</pre>
359 </section>
361 <section>
362 <h3 id="connections">$(gettext 'IP Connections')</h3>
364 <pre>
365 $(netstat -anp 2> /dev/null | sed -e '/UNIX domain sockets/,$d' \
366 -e 's#\([0-9]*\)/#<a href="boot.cgi?daemons=pid=\1">\1</a>/#')
367 </pre>
368 </section>
369 EOT
370 ;;
371 esac
373 xhtml_footer
374 exit 0