tazpanel view network.cgi @ rev 313

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