tazpanel view network.cgi @ rev 356

header.html: add menu extensions for panel, settings, boot and hardware (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Mar 23 08:38:11 2013 +0100 (2013-03-23)
parents 1602cb73097e
children 8ec43100e75e
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 's/.*ESSID:"\([^"]*\).*/\1/')
37 if echo "$SCAN" | grep -q Quality; then
38 QUALITY=$(echo $SCAN | sed 's/.*Quality:\([^ ]*\).*/\1/')
39 else
40 QUALITY="-"
41 fi
42 ENCRYPTION=$(echo $SCAN | sed 's/.*key:\([^ ]*\).*/\1/')
43 # Check encryption type
44 if echo "$SCAN" | grep -q WPA*; then
45 ENCRYPTION="WPA"
46 fi
47 if echo $SCAN | grep -q 'Mode:Managed'; then
48 AP="&ap=$(echo $SCAN | sed 's/.*Address: \([^ ]*\).*/\1/')"
49 else
50 AP=""
51 fi
52 # Connected or not connected...
53 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
54 fgrep -q inet && iwconfig $WIFI_INTERFACE | \
55 fgrep -q "ESSID:\"$ESSID\""; then
56 status=$(gettext 'Connected')
57 else
58 status="---"
59 fi
60 echo '<tr>'
61 echo "<td><a href=\"$SCRIPT_NAME?wifi&select=$ESSID&keytype=$ENCRYPTION&$AP\">
62 <img src='$IMAGES/wireless.png' />$ESSID</a></td>"
63 echo "<td>$QUALITY</td><td>$ENCRYPTION</td><td>$status $ip</td>"
64 echo '</tr>'
65 done
66 fi
67 cat << EOT
68 </tbody>
69 </table>
70 EOT
71 }
73 # Start a wifi connection
74 start_wifi() {
75 sed -i \
76 -e s'/^DHCP=.*/DHCP="yes"/' \
77 -e s'/^WIFI=.*/WIFI="yes"/' \
78 -e s'/^STATIC=.*/STATIC="no"/' /etc/network.conf
79 ifconfig $WIFI_INTERFACE up
80 iwconfig $WIFI_INTERFACE txpower auto
81 /etc/init.d/network.sh restart | log
82 sleep 2
83 }
85 # Actions commands before page is displayed
86 case " $(GET) " in
87 *\ start\ *)
88 # Here we sleep a bit to let udhcp get the lease before reloading
89 # the page with status
90 /etc/init.d/network.sh start | log
91 sleep 2 ;;
92 *\ stop\ *)
93 /etc/init.d/network.sh stop | log ;;
94 *\ restart\ *)
95 /etc/init.d/network.sh restart | log ;;
96 *\ start-wifi\ *) start_wifi ;;
97 *\ hostname\ *)
98 get_hostname="$(GET hostname)"
99 echo $(eval_gettext 'Changed hostname: $get_hostname') | log
100 echo "$get_hostname" > /etc/hostname ;;
101 esac
103 # Get values only now since they could have been modified by actions.
104 . /etc/network.conf
106 #
107 # Main Commands for pages
108 #
110 case " $(GET) " in
111 *\ scan\ *)
112 # Scan open ports
113 scan=$(GET scan)
114 xhtml_header
115 LOADING_MSG=$(gettext 'Scanning open ports...')
116 loading_msg
117 cat << EOT
118 <h2>$(eval_gettext 'Port scanning for $scan')</h2>
120 <pre>$(pscan -b $scan)</pre>
121 EOT
122 ;;
124 *\ eth\ *)
125 # Wired connections settings
126 xhtml_header
127 if [ "$(GET ip)" ]; then
128 DHCP=no
129 STATIC=no
130 [ -n "$(GET dhcp)" ] && DHCP=yes
131 [ -n "$(GET static)" ] && STATIC=yes
132 LOADING_MSG=$(gettext 'Setting up IP...')
133 loading_msg
134 sed -i \
135 -e s"/^INTERFACE=.*/INTERFACE=\"$(GET iface)\""/ \
136 -e s"/^DHCP=.*/DHCP=\"$DHCP\"/" \
137 -e s"/^STATIC=.*/STATIC=\"$STATIC\"/" \
138 -e s"/^NETMASK=.*/NETMASK=\"$(GET netmask)\"/" \
139 -e s"/^GATEWAY=.*/GATEWAY=\"$(GET gateway)\"/" \
140 -e s"/^DNS_SERVER=.*/DNS_SERVER=\"$(GET dns)\"/" \
141 -e s"/^IP=.*/IP=\"$(GET ip)\"/" /etc/network.conf
142 /etc/init.d/network stop | log
143 sleep 2
144 /etc/init.d/network start | log
145 . /etc/network.conf
146 fi
147 cat << EOT
148 <h2>$(gettext 'Ethernet connection')</h2>
150 <p>$(gettext "Here you can configure a wired connection using DHCP to \
151 automatically get a random IP or configure a static/fixed IP")</p>
153 <section>
154 <h3>$(gettext 'Configuration')</h3>
155 <form method="get" action="$SCRIPT_NAME">
156 <input type="hidden" name="eth" />
157 <table>
158 <thead>
159 <tr>
160 <td>$(gettext 'Name')</td>
161 <td>$(gettext 'Value')</td>
162 </tr>
163 </thead>
164 <tbody>
165 <tr>
166 <td>$(gettext 'Interface')</td>
167 <td><input type="text" name="iface" size="20" value="$INTERFACE" /></td>
168 </tr>
169 <tr>
170 <td>$(gettext 'IP address')</td>
171 <td><input type="text" name="ip" size="20" value="$IP" /></td>
172 </tr>
173 <tr>
174 <td>$(gettext 'Netmask')</td>
175 <td><input type="text" name="netmask" size="20" value="$NETMASK" /></td>
176 </tr>
177 <tr>
178 <td>$(gettext 'Gateway')</td>
179 <td><input type="text" name="gateway" size="20" value="$GATEWAY" /></td>
180 </tr>
181 <tr>
182 <td>$(gettext 'DNS server')</td>
183 <td><input type="text" name="dns" size="20" value="$DNS_SERVER" /></td>
184 </tr>
185 </tbody>
186 </table>
187 <input type="submit" name="static" value="$(gettext 'Activate (static)')">
188 <input type="submit" name="dhcp" value="$(gettext 'Activate (DHCP)')">
189 <input type="submit" name="disable" value="$(gettext 'Disable')">
190 </form>
191 </section>
193 <section>
194 <h3>$(gettext 'Configuration file')</h3>
196 <p>$(gettext "These values are the ethernet settings in the main \
197 /etc/network.conf configuration file")</p>
198 <pre>
199 $(grep ^[A-V] /etc/network.conf | syntax_highlighter conf)
200 </pre>
201 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
202 <img src="$IMAGES/edit.png" />$(gettext 'Manual Edit')</a>
203 </section>
204 EOT
205 ;;
206 *\ wifi\ *)
207 # Wireless connections settings
208 xhtml_header
209 LOADING_MSG=$(gettext 'Scanning wireless interface...')
210 loading_msg
211 . /etc/network.conf
212 cat << EOT
213 <h2>$(gettext 'Wireless connection')</h2>
214 <div id="actions">
215 <a class="button" href="$SCRIPT_NAME?wifi&start-wifi=start-wifi">
216 <img src="$IMAGES/start.png" />$(gettext 'Start')</a>
217 <a class="button" href="$SCRIPT_NAME?wifi&stop=stop">
218 <img src="$IMAGES/stop.png" />$(gettext 'Stop')</a>
219 <a class="button" href="$SCRIPT_NAME?wifi=scan">
220 <img src="$IMAGES/recharge.png" />$(gettext 'Scan')</a>
221 </div>
222 $(detect_wifi_networks)
223 EOT
224 WIFI_AP="$(GET ap)"
225 WIFI_KEY="$(GET key)"
226 case "$(GET keytype)" in
227 ''|off) WIFI_KEY_TYPE=none ;;
228 *) WIFI_KEY_TYPE=any ;;
229 esac
230 if [ "$(GET essid)" ]; then
231 /etc/init.d/network.sh stop | log
232 sed -i \
233 -e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$(GET essid)\""/ \
234 -e s"/^WIFI_KEY=.*/WIFI_KEY=\"$WIFI_KEY\"/" \
235 -e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$WIFI_KEY_TYPE\"/" \
236 -e s"/^WIFI_AP=.*/WIFI_AP=\"$WIFI_AP\"/" \
237 /etc/network.conf
238 . /etc/network.conf
239 start_wifi
240 fi
241 # ESSID names are clickable
242 if [ "$(GET select)" ]; then
243 if [ "$(GET select)" != "$WIFI_ESSID" ]; then
244 WIFI_KEY=""
245 fi
246 WIFI_ESSID="$(GET select)"
247 fi
248 cat << EOT
249 <section>
250 <h3>$(gettext 'Connection')</h3>
251 <form method="get" action="$SCRIPT_NAME">
252 <input type="hidden" name="connect-wifi" />
253 $(table_start)
254 <thead>
255 <tr>
256 <td>$(gettext 'Name')</td>
257 <td>$(gettext 'Value')</td>
258 </tr>
259 </thead>
260 <tr>
261 <td>$(gettext 'Wifi name (ESSID)')</td>
262 <td><input type="text" name="essid" size="30" value="$WIFI_ESSID" /></td>
263 </tr>
264 <tr>
265 <td>$(gettext 'Password (Wifi key)')</td>
266 <td><input type="password" name="key" size="30" value="$WIFI_KEY" /></td>
267 </tr>
268 <tr>
269 <td>$(gettext 'Encryption type')</td>
270 <td><input type="text" name="keytype" size="30" value="$WIFI_KEY_TYPE" /></td>
271 </tr>
272 <tr>
273 <td>$(gettext 'Access point')</td>
274 <td><input type="text" name="ap" size="30" value="$WIFI_AP" /></td>
275 </tr>
276 $(table_end)
277 <input type="submit" name="wifi" value="$(gettext 'Configure')" />
278 </form>
279 </section>
281 <section>
282 <h3>$(gettext 'Configuration file')</h3>
284 <p>$(gettext "These values are the wifi settings in the main /etc/network.conf \
285 configuration file")</p>
287 <pre>$(grep ^WIFI /etc/network.conf | syntax_highlighter conf)</pre>
289 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
290 <img src="$IMAGES/edit.png" />$(gettext 'Manual Edit')</a>
291 </section>
293 <section>
294 <h3>$(gettext 'Output of iwconfig')</h3>
296 <pre>$(iwconfig)</pre>
297 </section>
298 EOT
299 ;;
300 *)
301 # Main Network page starting with a summary
302 xhtml_header
303 hostname=$(cat /etc/hostname)
304 cat << EOT
305 <h2>$(gettext 'Networking')</h2>
307 <p>$(gettext 'Manage network connections and services')</p>
309 <section>
310 <div id="actions">
311 <div class="float-left">
312 <a class="button" href="$SCRIPT_NAME?start">
313 <img src="$IMAGES/start.png" />$(gettext 'Start')</a>
314 <a class="button" href="$SCRIPT_NAME?stop">
315 <img src="$IMAGES/stop.png" />$(gettext 'Stop')</a>
316 <a class="button" href="$SCRIPT_NAME?restart">
317 <img src="$IMAGES/recharge.png" />$(gettext 'Restart')</a>
318 </div>
319 <div class="float-right">
320 $(gettext 'Configuration:')
321 <a class="button" href="index.cgi?file=/etc/network.conf">network.conf</a>
322 <a class="button" href="$SCRIPT_NAME?eth">Ethernet</a>
323 <a class="button" href="$SCRIPT_NAME?wifi">Wireless</a>
324 </div>
325 </div>
327 $(list_network_interfaces)
328 </section>
330 <section>
331 <h3 id="hosts">$(gettext 'Hosts')</h3>
333 <pre>$(cat /etc/hosts)</pre>
335 <a class="button" href="index.cgi?file=/etc/hosts&action=edit">
336 <img src="$IMAGES/edit.png" />$(gettext 'Edit hosts')</a>
337 </section>
339 <section>
340 <h3>$(gettext 'Hostname')</h3>
342 <form method="get" name="$SCRIPT_NAME">
343 <input type="text" name="hostname" value="$hostname" />
344 <input type="submit" value="$(gettext 'Change hostname')" />
345 </form>
346 </section>
348 <section>
349 <h3 id="ifconfig">$(gettext 'Output of ifconfig')</h3>
351 <pre>$(ifconfig)</pre>
352 </section>
354 <section>
355 <h3 id="routing">$(gettext 'Routing table')</h3>
357 <pre>$(route -n)</pre>
358 </section>
360 <section>
361 <h3 id="dns">$(gettext 'Domain name resolution')</h3>
363 <pre>$(cat /etc/resolv.conf)</pre>
364 </section>
366 <section>
367 <h3 id="arp">$(gettext 'ARP table')</h3>
369 <pre>$(arp)</pre>
370 </section>
372 <section>
373 <h3 id="connections">$(gettext 'IP Connections')</h3>
375 <pre>
376 $(netstat -anp 2> /dev/null | sed -e '/UNIX domain sockets/,$d' \
377 -e 's#\([0-9]*\)/#<a href="boot.cgi?daemons=pid=\1">\1</a>/#')
378 </pre>
379 </section>
380 EOT
381 ;;
382 esac
384 xhtml_footer
385 exit 0