+ cf server side, CGIs
+ keep CGIs really small, very productive
+ BASIC style string processing versus regexp
-
+ + CGI wring, regexp, sed is painfully slow, so minimise it
#!/bin/sh
# setline.cgi
-# David Rowe 12 Dec 2010
+# David Rowe 9 March 2011
+#
+# CGI to set the text in "file" from "this" to "that"
+#
+# Test on host with:
+#
+# $ export QUERY_STRING='file=test.txt&this=IPADDRESS=&that="192.168.1.31"'; ./setline.cgi
#
-# CGI to set the text on a particular line
cat <<EOF
Content-type: text/html
<body>
EOF
-LINE=`echo "$QUERY_STRING" | sed -n "s/.*line=//p"`
-echo $LINE
-
+file=`echo $QUERY_STRING | grep -oe "file=[^&?]*" | sed -n "s/file=//p"`
+this=`echo $QUERY_STRING | grep -oe "this=[^&?]*" | sed -n "s/this=//p" | sed "s/%22/\"/g"`
+that=`echo $QUERY_STRING | grep -oe "that=[^&?]*" | sed -n "s/that=//p" | sed "s/%22/\"/g"`
+echo $QUERY_STRING
+echo $file
+echo $this
+echo $that
+if [ -f $file ]; then
+ sed -i "s/$this.*/$this$that/g" $file
+else
+ echo "$file does not exist"
+fi
+
cat <<EOF
</body>
</html>
+++ /dev/null
-#!/usr/bin/speedy -w
-# -*-perl-*-
-
-use lib qw(/usr/share/perl5/smokeping);
-use CGI::Carp qw(fatalsToBrowser);
-
-use Smokeping 2.003006;
-
-Smokeping::cgi("/etc/smokeping/config");
-
-=head1 NAME
-
-smokeping.cgi - SmokePing webfrontend
-
-=head1 OVERVIEW
-
-This script acts as a 'website' for your SmokePing monitoring operation. It
-presents the targets you are looking at in a tree structure and draws graphs
-as they are required by people looking at the pages.
-
-=head1 DESCRIPTION
-
-To get B<smokeping.cgi> going, you need a webserver which allows you to run
-cgi scripts. The system must be setup so that the B<smokeping.cgi> is
-allowed to write to the image caching area as defined in the config file.
-
-This script runs with normal perl. B<BUT> it will appear to be very slow,
-because it does a lot of things when starting up. So if the script has to be
-started a fresh on every click, this is both slow and a tough thing for your
-webserver. I therefore strongly recomment using SpeedyCGI.
-
-Please refer to the installation document for detailed setup instructions.
-
-=head1 SETUP
-
-When installing SmokePing, this file has to be adjusted to fit your
-local system. Three paths have to be entered.
-
- use lib qw(/usr/pack/rrdtool-1.0.33-to/lib/perl);
-
-One pointing to your B<rrdtool> installation
-
- use lib qw(/home/oetiker/public_html/smokeping/lib);
-
-One pointing to the place where you have installed the SmokePing libraries
-
- use Smokeping;
-
- Smokeping::cgi("/home/oetiker/.smokeping/config");
-
-The third path is the argument to the Smokeping::cgi command. It points to
-the SmokePing configuration file.
-
-=head1 COPYRIGHT
-
-Copyright (c) 2001 by Tobias Oetiker. All right reserved.
-
-=head1 LICENSE
-
-This program is free software; you can redistribute it
-and/or modify it under the terms of the GNU General Public
-License as published by the Free Software Foundation; either
-version 2 of the License, or (at your option) any later
-version.
-
-This program is distributed in the hope that it will be
-useful, but WITHOUT ANY WARRANTY; without even the implied
-warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-PURPOSE. See the GNU General Public License for more
-details.
-
-You should have received a copy of the GNU General Public
-License along with this program; if not, write to the Free
-Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
-02139, USA.
-
-=head1 AUTHOR
-
-Tobias Oetiker E<lt>tobi@oetiker.chE<gt>
-
-=cut
<table align="center" width=600 border=0>
<tr><td colspan="2" align="left" valign="top"><h2>Network</h2></td></tr>
<tr>
- <td><input type="radio" name="static" value="no" onClick="doStatic()">Static</td>
- <td><input type="radio" name="dhcp" value="yes" onClick="doDHCP()">DHCP</td>
+ <td><input type="radio" id="static" name="dhcp" value="no" onClick="doStatic()">Static</td>
+ <td><input type="radio" id="dhcp" name="dhcp" value="yes" onClick="doDHCP()">DHCP</td>
</tr>
<tr><td>IP Address:</td><td><input type="text" name="ipaddress" onBlur="isIP(this)"></td></tr>
<tr><td>Netmask:</td><td><input type="text" name="netmask" onBlur="isIP(this)"></td></tr>
<td>Internet Connection:</td>
<td><div id="internet"></div></td>
</tr>
- <tr><td><input type="submit" value="Apply"></td></tr>
+ <tr><td></td><td><input type="button" style="width:20%" value="Apply" onClick="onClickApply()"></td></tr>
</table>
</form>
-
</tr>
</table>
*/
var dhcp;
+var ipaddress;
+var netmask;
+var gateway;
+var dns;
+var backdoor;
// Called when we load page
if (line.indexOf("IPADDRESS=") != -1) {
var s = line.split('"');
- var ipaddress = s[1];
+ ipaddress = s[1];
document.network.ipaddress.value = ipaddress;
}
if (line.indexOf("NETMASK=") != -1) {
var s = line.split('"');
- var netmask = s[1];
+ netmask = s[1];
document.network.netmask.value = netmask;
}
if (line.indexOf("GATEWAY=") != -1) {
var s = line.split('"');
- var gateway = s[1];
+ gateway = s[1];
document.network.gateway.value = gateway;
}
if (line.indexOf("DNS=") != -1) {
var s = line.split('"');
- var dns = s[1];
+ dns = s[1];
document.network.dns.value = dns;
}
if (line.indexOf("BACKDOOR=") != -1) {
var s = line.split('"');
- var backdoor = s[1];
+ backdoor = s[1];
document.network.backdoor.value = backdoor;
}
}
return ip;
}
+
+
+// called when Apply button pressed
+
+function onClickApply() {
+
+ // change network type, DHCP or static
+
+ if (document.network.dhcp.value == "yes") {
+
+ if (dhcp = "no") {
+ // disable network-state service and endable network service
+ downloadUrl(/cgi-bin/setservice.cgi?action=enabledhcp,null);
+ }
+ else {
+ // dhcp already running, restart service
+ downloadUrl(/cgi-bin/setservice.cgi?action=restartdhcp,null);
+ }
+ }
+ else {
+
+ if (dhcp = "yes") {
+ // disable network service and enable network-static service
+ downloadUrl(/cgi-bin/setservice.cgi?action=enablestatic,null);
+ }
+
+ // change network-static file variables
+
+ if (document.network.ipaddress.value != ipaddress) {
+ var new_ipaddress = document.network.ipaddress.value;
+ var url = '/cgi-bin/setline.cgi?file=/etc/init.d/network-static&this=IPADDRESS=&that="' + new_ipaddress + '"';
+ downloadUrl(url,null);
+ }
+
+ if (document.network.netmask.value != netmask) {
+ var new_netmask = document.network.netmask.value;
+ var url = '/cgi-bin/setline.cgi?file=/etc/init.d/network-static&this=NETMASK=&that="' + new_netmask + '"';
+ downloadUrl(url,null);
+ }
+
+ if (document.network.gateway.value != gateway) {
+ var new_ = document.network.gateway.value;
+ var url = '/cgi-bin/setline.cgi?file=/etc/init.d/network-static&this=GATEWAY=&that="' + new_gateway + '"';
+ downloadUrl(url,null);
+ }
+
+ if (document.network.dns.value != dns) {
+ var new_ = document.network.dns.value;
+ var url = '/cgi-bin/setline.cgi?file=/etc/init.d/network-static&this=DNS=&that="' + new_dns + '"';
+ downloadUrl(url,null);
+ }
+
+ if (document.network.backdoor.value != backdoor) {
+ var new_ = document.network.backdoor.value;
+ var url = '/cgi-bin/setline.cgi?file=/etc/init.d/network-backup&this=BACKDOOR=&that="' + new_backdoor + '"';
+ downloadUrl(url,null);
+ downloadUrl(/cgi-bin/setservice.cgi?action=restartbackup,null);
+ }
+
+ // finally restart network-static service with new parameters
+
+ downloadUrl(/cgi-bin/setservice.cgi?action=restartstatic,null);
+ }
+}