DynIPHosting

From 3DN

Jump to: navigation, search
This site is part of the 3DN Network. Every content on the 3DN Network is subject to the [3DN Terms of Service]. Other sites in the 3DN Network include: 3DN Politics 3DN Technology 3DN Voetbal 3DN Politicap and Dutchie

Here we'll document the DynIPHosting tool. The DynIPHosting tool is really highly specific; it's written only for 3DN's own purposes but perhaps you'll get some inspiration out of it and extend it to also work in your own setup.

The Problem

Running a rather large amount of [websites] on my server at home becomes quite tedious when the IP address they run on keeps changing since my provider won't give me anything even resembling a static IP address. Even worse, sometimes it appears that the ISP doesn't abide by the DHCP standards, resulting in my home IP address changing on rather unpredictable moments. So I decided to develop a [simple tool] that would:

  • Monitor for me if my IP address has changed, and if so,
  • update my DNS records and the Apache configuration files
  • restart Apache to activate the changes

Lots of additional checks could (and probably will) be made, but the above is the bare bone desired functionality.

The Approach

I typically like visualizing the results of my tools through some sort of webinterface. I also like to store data that the tool uses in, what else, a relational database. Because I typically write a million and 1 tools at the same time, setting up a new website, new database etc. is rather repetitive for me, so I decided to make a website template and put it in Subversion, it has a few basic PHP classes, login mechanism, acceptable CSS layout, the works. To getting the initial site and database up and running took very little effort. I used [BannerSketch] to whip up a banner very quickly (yeah even faster than I could do it with Gimp :) in order to brand the tool somewhat and added some links to the top to give us some navigation (we won't be needing much navigation anyway as this will mostly be a status viewing site). So the initial screen looks like this:

Screenshot-DynIPHosting - Mozilla Firefox.png
Dyniplogin.png

So in the website template there's a class call DBObject which I use rather frequently. It's a nice base class to derive some of the classes I need from. The first class we need is a class called 'Website'. I'll be maintaining some data for all the websites I plan to keep track of in it:

    class Website extends DBObject {
        /**
         * Constructor for Website
         */
        function __construct () {
            $this->fields = array (
                "configfile" => "varchar(255)|File",
                "description" => "varchar(255)|Description",
                "ipaddress" => "char(15)"
            );
            $this->tablename = "websites";
            parent::__construct();
        }
    }

Having defined this class, I'll add a simple object listing for Website objects to the main menu:

mysql> INSERT INTO links SET
    -> id=0,
    -> linkcat=1,
    -> url='/websites.php',
    -> shortname='Sites',
    -> alt='Sites',
    -> comment='Sites',
    -> submitter=1,
    -> submitgroup=1,
    -> permissions=14;
Query OK, 1 ROW affected (0.02 sec)

Now I need to add the URL mentioned in this INSERT (/websites.php) to the documentroot and in it I'll use another frequently used class that's much older already, DBList:

    $l = new DBList();
    $l->setattribs (array (
        maxlines => 17,
        rounded => 1,
        title => "Websites",
        caption => "Websites",
        summary => "Websites",
        sortcolumn => $sortcolumn,
        srchcolumn => $srchcolumn,
        setnum => $setnum,
        format => $format,
        sortorder => $sortorder,
        buttontext => '',
        buttonaction => 'websites.php',
        srchqry => $srchqry)
    );
 
    $l->setquery (array (
        "select" => "id,configfile,description,ipaddress",
        "from" => "websites",
        )
    );
 
    $page .= $l->output();

And I need to create the database table 'websites' used in both the DBList() example and the Websites class:

CREATE TABLE websites (
 id INT NOT NULL AUTO_INCREMENT,
 configfile VARCHAR(255),
 description VARCHAR(255), 
 ipaddress CHAR(15), 
 submitter INT, 
 submitgroup INT DEFAULT 2, 
 permissions INT, 
 PRIMARY KEY(id)) TYPE="innodb";

Now we're pretty much set with the webinterface for now as we're able to sign up, login and get a listing of the websites we're hosting. We still need to populate the websites table however and that's where some timed events that have nothing to do with the website come in. Oh yeah... you really need to sign up and log in in order to see the website listing. The DBListing class is really kind of old and, maybe because of that, still linked to some session variables such as the sort column, the sort order etc. I might fix that at a later time but I plan to rewrite the DBList class anyway so it works nicer together with the DBObject class. If you really would enjoy seeing my prattling, just sign up and I promise I won't sell your email address :P

The Automated Scripts

So yeah... I intend to use the PHP CLI for creating some commandline scripts. Most coders would call me crazy for doing this but hey, it serves a purpose, I can reuse my database object, my debugging object, my DBObject etc. without having to rewrite them to Perl, which would indeed be nicer for commandline purposes. Since this is a rather quick and dirty tool which I intend to have working in a raw form in a day, I'll choose Perl.

So in the bin directory we have a script called scanwsites.php:

#!/usr/bin/php
<?
    $dir = "/etc/apache2/sites-enabled";
 
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "checking: $file\n";
        }
    }
 
?>

<google uid="C08" position="right"></google>

Personal tools
Google AdSense