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