tazpanel view network.cgi @ rev 108

network.cgi: randow fixes (start-stop wifi) and improvment
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 15 08:51:30 2011 +0200 (2011-04-15)
parents 306b0bf62bb8
children b898c9887a62
line source
1 #!/bin/sh
2 #
3 # Network configuration CGI interface
4 #
6 # Common functions from libtazpanel
7 . lib/libtazpanel
8 get_config
9 header
11 # Include gettext helper script.
12 . /usr/bin/gettext.sh
14 # Export package name for gettext.
15 TEXTDOMAIN='tazpanel'
16 export TEXTDOMAIN
18 TITLE="- Network"
20 # Catch ESSIDs and format output for GTK tree. We get the list of
21 # networks by Cell and without spaces.
22 detect_wifi_networks()
23 {
24 table_start
25 cat << EOT
26 <thead>
27 <tr>
28 <td>$(gettext "Name")</td>
29 <td>$(gettext "Quality")</td>
30 <td>$(gettext "Encryption")</td>
31 <td>$(gettext "Status")</td>
32 </tr>
33 </thead>
34 EOT
35 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
36 ifconfig $WIFI_INTERFACE up
37 for i in `iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}'`
38 do
39 SCAN=`iwlist $WIFI_INTERFACE scan last | \
40 awk '/(Cell|ESS|Qual|Encry|IE: WPA)/ {print}' | \
41 sed s/"Cell "/Cell-/ | grep -A 5 "$i"`
42 ESSID=`echo $SCAN | cut -d '"' -f 2`
43 if echo "$SCAN" | grep -q Quality; then
44 QUALITY=`echo $SCAN | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/'`
45 else
46 QUALITY="-"
47 fi
48 ENCRYPTION=`echo $SCAN | sed 's/.*key:\([^ ]*\).*/\1/'`
49 # Check encryption type
50 if echo "$SCAN" | grep -q WPA; then
51 ENCRYPTION="${ENCRYPTION} (WPA)"
52 fi
53 # Connected or not connected...
54 if ifconfig | grep -A 1 $WIFI_INTERFACE | \
55 fgrep -q inet && iwconfig $WIFI_INTERFACE | \
56 grep ESSID | fgrep -q -w "$ESSID"; then
57 status=$(gettext "Connected")
58 else
59 status="---"
60 fi
61 echo '<tr>'
62 echo "<td><img src='$IMAGES/wireless.png' />$ESSID</td>"
63 echo "<td>$QUALITY</td><td>$ENCRYPTION</td><td>$status $ip</td>"
64 echo '</tr>'
65 done
66 fi
67 table_end
68 }
70 # Actions commands before page is displayed
71 case " $(GET) " in
72 *\ start\ *)
73 # Here we sleep a bit to let udhcp get the lease before reloading
74 # page with status
75 /etc/init.d/network.sh start | log
76 sleep 2 ;;
77 *\ stop\ *)
78 /etc/init.d/network.sh stop | log ;;
79 *\ start-wifi\ *)
80 sed -i \
81 -e s'/^DHCP=.*/DHCP="yes"/' \
82 -e s'/^WIFI=.*/WIFI="yes"/' \
83 -e s'/^STATIC=.*/STATIC="no"/'/etc/network.conf
84 /etc/init.d/network.sh start | log
85 sleep 2 ;;
86 *\ hostname\ *)
87 echo $(gettext "Changed hostname:") $(GET hostname) | log
88 echo "$(GET hostname)" > /etc/hostname ;;
89 esac
91 . /etc/network.conf
93 #
94 # Main Commands for pages
95 #
97 case " $(GET) " in
98 *\ eth\ *)
99 # Wired connections settings
100 xhtml_header
101 if [ "$(GET ip)" ]; then
102 LOADING_MSG=$(gettext "Setting up static IP...")
103 loading_msg
104 sed -i \
105 -e s"/^INTERFACE=.*/INTERFACE=\"$(GET iface)\""/ \
106 -e s'/^DHCP=.*/DHCP="no"/' \
107 -e s'/^WIFI=.*/WIFI="no"/' \
108 -e s'/^STATIC=.*/STATIC="yes"/' \
109 -e s"/^NETMASK=.*/NETMASK=\"$(GET netmask)\"/" \
110 -e s"/^GATEWAY=.*/GATEWAY=\"$(GET gateway)\"/" \
111 -e s"/^DNS_SERVER=.*/DNS_SERVER=\"$(GET dns)\"/" \
112 -e s"/^IP=.*/IP=\"$(GET ip)\"/" /etc/network.conf
113 /etc/init.d/network stop | log
114 sleep 2
115 /etc/init.d/network start | log
116 fi
117 . /etc/network.conf
118 cat << EOT
119 <h2>`gettext "Ethernet connection`</h2>
121 <h3>$(gettext "Setup a static IP")</h3>
122 <form method="get" action="$SCRIPT_NAME">
123 <input type="hidden" name="eth" />
124 $(table_start)
125 <thead>
126 <tr>
127 <td>$(gettext "Name")</td>
128 <td>$(gettext "Value")</td>
129 </tr>
130 </thead>
131 <tr>
132 <td>$(gettext "Interface")</td>
133 <td><input type="text" name="iface" size="20" value="$INTERFACE" /></td>
134 </tr>
135 <tr>
136 <td>$(gettext "IP address")</td>
137 <td><input type="text" name="ip" size="20" value="$IP" /></td>
138 </tr>
139 <tr>
140 <td>$(gettext "Netmask")</td>
141 <td><input type="text" name="netmask" size="20" value="$NETMASK" /></td>
142 </tr>
143 <tr>
144 <td>$(gettext "Gateway")</td>
145 <td><input type="text" name="gateway" size="20" value="$GATEWAY" /></td>
146 </tr>
147 <tr>
148 <td>$(gettext "DNS server")</td>
149 <td><input type="text" name="dns" size="20" value="$DNS_SERVER" /></td>
150 </tr>
151 $(table_end)
152 <input type="submit" value="`gettext "Activate"`">
153 </form>
155 <h3>$(gettext "Configuration file")</h3>
156 <p>
157 $(gettext "These values are the ethernet settings in the main
158 /etc/network.conf configuration file")
159 </p>
160 <pre>
161 $(grep ^[A-V] /etc/network.conf | syntax_highlighter conf)
162 </pre>
163 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
164 <img src="$IMAGES/edit.png" />$(gettext "Manual Edit")</a>
165 EOT
166 ;;
167 *\ wifi\ *)
168 # Wireless connections settings
169 xhtml_header
170 LOADING_MSG=$(gettext "Scanning wireless interface...")
171 loading_msg
172 cat << EOT
173 <h2>`gettext "Wireless connection`</h2>
174 <div id="actions">
175 <a class="button" href="$SCRIPT_NAME?wifi&start-wifi=start-wifi">
176 <img src="$IMAGES/start.png" />$(gettext "Start")</a>
177 <a class="button" href="$SCRIPT_NAME?wifi&stop=stop">
178 <img src="$IMAGES/stop.png" />$(gettext "Stop")</a>
179 <a class="button" href="$SCRIPT_NAME?wifi=scan">
180 <img src="$IMAGES/recharge.png" />$(gettext "Scan")</a>
181 </div>
182 $(detect_wifi_networks)
183 EOT
184 cat << EOT
185 <h3>$(gettext "Configuration file")</h3>
186 <p>
187 $(gettext "These values are the wifi settings in the main
188 /etc/network.conf configuration file")
189 </p>
190 <pre>
191 $(grep ^WIFI /etc/network.conf | syntax_highlighter conf)
192 </pre>
193 <a class="button" href="index.cgi?file=/etc/network.conf&action=edit">
194 <img src="$IMAGES/edit.png" />$(gettext "Manual Edit")</a>
196 <h3>$(gettext "Output of") iwconfig</h3>
197 <pre>
198 $(iwconfig)
199 </pre>
200 EOT
201 ;;
202 *)
203 # Main Network page starting with a summary
204 xhtml_header
205 hostname=$(cat /etc/hostname)
206 cat << EOT
207 <h2>`gettext "Networking`</h2>
208 <p>
209 `gettext "Manage network connections and services`
210 </p>
211 <div id="actions">
212 <div class="float-left">
213 `gettext "Connection:"`
214 <a class="button" href="$SCRIPT_NAME?start">
215 <img src="$IMAGES/start.png" />$(gettext "Start")</a>
216 <a class="button" href="$SCRIPT_NAME?stop">
217 <img src="$IMAGES/stop.png" />$(gettext "Stop")</a>
218 </div>
219 <div class="float-right">
220 `gettext "Configuration:"`
221 <a class="button" href="index.cgi?file=/etc/network.conf">network.conf</a>
222 <a class="button" href="$SCRIPT_NAME?eth">Ethernet</a>
223 <a class="button" href="$SCRIPT_NAME?wifi">Wireless</a>
224 </div>
225 </div>
227 $(list_network_interfaces)
229 <h3>$(gettext "Hosts")</h3>
230 <pre>
231 $(cat /etc/hosts)
232 </pre>
233 <a class="button" href="index.cgi?file=/etc/hosts&action=edit">
234 <img src="$IMAGES/edit.png" />$(gettext "Edit hosts")</a>
236 <h3>$(gettext "Hostname")</h3>
237 <form method="get" name="$SCRIPT_NAME"
238 <input type="text" name="hostname" value="$hostname" />
239 <input type="submit" value="$(gettext "Change hostname")"
240 </form>
243 <h3>$(gettext "Output of ") ifconfig</h3>
244 <pre>
245 $(ifconfig)
246 </pre>
248 <h3>`gettext "Routing table"`</h3>
249 <pre>
250 $(route -n)
251 </pre>
253 <h3>`gettext "Domain name resolution"`</h3>
254 <pre>
255 $(cat /etc/resolv.conf)
256 </pre>
258 <h3>`gettext "ARP table"`</h3>
259 <pre>
260 $(arp)
261 </pre>
262 EOT
263 ;;
264 esac
266 xhtml_footer
267 exit 0