On Mon, 2003-06-16 at 01:14, Daevid Vincent wrote:
> So my question is, does anyone have some code (PHP preferred obviously,
> but any language should work or be portable) that will take a file of
> the form below and split it into the chunks required. Or anything even
> close? For example, a 'chunk' would be between the { and } . I'm sure I
> can figure this out, but before I go re-inventing the wheel, thought
> someone might have done this already to some degree.
>  
> lease 192.168.1.3 {
>   starts 1 2003/06/16 07:38:55;
>   ends 1 2003/06/16 07:48:55;
>   binding state active;
>   next binding state free;
>   hardware ethernet 00:80:45:31:d8:29;
> }
> lease 192.168.1.6 {
>   starts 1 2003/06/16 07:39:27;
>   ends 1 2003/06/16 07:49:27;
>   binding state active;
>   next binding state free;
>   hardware ethernet 48:54:e8:26:23:38;
>   uid "\001HT\350&#8";
>   client-hostname "jme";
> }

I just banged this off the top of my head. Little error checking but you
should be able to see where to add it. It should be tweakable for what 
you need, at least to get you going.

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

class LeaseParser {
    // List designators valid within lease blocks here.
    // Add/modify/remove to suit.
    var $_valid_items = 
    array('starts 1',
          'ends 1',
          'binding state active',
          'next binding state',
          'hardware ethernet',
          'uid',
          'client-hostname');

    // Stores any errors we generate during the run. Theses can be
output
    // directly by calling showErrors();
    var $_errors;

    // Storage for the contents of the lease file while it's being
working on.
    var $_file_contents;

    // Stores the parsed lease array.
    var $_leases;

    // Put any options you want to define in here, and use setOption()
to modify
    // it.
    var $_options = 
    array('file_name' => '');


    // Private method. Attempts to load the leases file.
    function _loadFile() {
        if (!$fp = fopen($this->_options['file_name'], 'r')) {
            $this->_errors[] = "LeaseParser::_loadFile(): Failed to load
{$this->_options['file_name']}";
            return false;
        }

        if (!$file = fread($fp, filesize($this->_options['file_name'])))
{
            $this->_errors[] = "LeaseParser::_loadFile(): Failed to read
{$this->_options['file_name']}";
            return false;
        }

        $this->_file_contents =& $file;

        return true;
    }

    
    // Private method. Attempts to parse the loaded file contents into a
usable format.
    function _parseFile() {
        if (empty($this->_file_contents)) {
            $this->_errors[] = "LeaseParser::_parseFile(): No leases
file data to parse.";
            return false;
        }

        $f_temp = explode('}', $this->_file_contents);

        $leases = array();

        foreach ($f_temp as $lease_record) {
//             echo "<hr>";
//             print_r($lease_record);
            if (!preg_match('/^\s*lease
(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}))/i',
$lease_record, $lease_matches)) {
                continue;
            }
//             echo "{$lease_matches[1]}\n";
            $leases[$lease_matches[1]] = array();
            $working_lease =& $leases[$lease_matches[1]];
            foreach ($this->_valid_items as $valid_item) {
                $matches = array();
                $pattern = "/ *({$valid_item}) (.*)\n/i";
//                 echo "Checking for $valid_item; $pattern\n";
                if (!preg_match_all($pattern, $lease_record, $matches,
PREG_SET_ORDER)) {
                    continue;
                }
//                 echo "-----\n";
//                 print_r($matches);
                $working_lease[$matches[0][1]] = $matches[0][2];
            }
        }

//         print_r($leases);

        $this->_leases = $leases;

        return true;
    }


    // If a leases file has been loaded and parsed, this will return
    // the generated array.
    function getLeases() {
        if (!empty($this->_leases)) {
            return $this->_leases;
        }
    }


    // Called by the user to load and parse a lease file. The filename
    // must have already been set.
    function parse() {
        if (!$this->_loadFile()) {
            $this->_errors[] = "LeaseParser::parse(): Could not load
leases file.";
            return false;
        }
        if (!$this->_parseFile()) {
            $this->_errors[] = "LeaseParser::parse(): Could not parse
leases file.";
            return false;
        }
        return true;
    }


    // Sets an option. The option must be defined in $_options.
    function setOption($option, $value) {
        if (!isset($this->_options[$option])) {
            $this->_errors[] = "LeaseParser::setOption(): Attempt made
to set illegal option.";
            return false;
        }
        $old_val = $this->_options[$option];
        $this->_options[$option] = $value;
        return $old_val;
    }


    // Dump any errors to output.
    function showErrors() {
        if (empty($this->_errors)) {
            return false;
        }
        print_r($this->_errors);
        return true;
    }
    

    // Null contstructor.
    function LeaseParser() {
    }
}

$lp =& new LeaseParser();
$lp->setOption('file_name', 'leases.txt');
$lp->parse();
$leases = $lp->getLeases();
print_r($leases);
$lp->showErrors();
?>

> when the script is finished I'll post it up for anyone to use on their
> own servers...
>  
> by the way, does anyone know why "arp -n" (what i'm using) shows
> machines that are NOT even turned on? like my notebook shows up despite
> having been off for several hours? Is there a way to get a real time
> list of the machines on my network? I thought that's what arp did, but
> apparently not. :(
>  
> d

IIRC it's because arp deals with the kernel cache, not necessarily the
live data. Wouldn't bet my life on it though.


Hope this helps,

Torben


-- 
 Torben Wilson <[EMAIL PROTECTED]>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====-----




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to