tazpanel view network.cgi @ rev 240

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