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