#!/usr/bin/php
<?php
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Elastix version 2.0.4                                                |
  | http://www.elastix.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | Cdla. Nueva Kennedy Calle E 222 y 9na. Este                          |
  | Telfs. 2283-268, 2294-440, 2284-356                                  |
  | Guayaquil - Ecuador                                                  |
  | http://www.palosanto.com                                             |
  +----------------------------------------------------------------------+
  | The contents of this file are subject to the General Public License  |
  | (GPL) Version 2 (the "License"); you may not use this file except in |
  | compliance with the License. You may obtain a copy of the License at |
  | http://www.opensource.org/licenses/gpl-license.php                   |
  |                                                                      |
  | Software distributed under the License is distributed on an "AS IS"  |
  | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
  | the License for the specific language governing rights and           |
  | limitations under the License.                                       |
  +----------------------------------------------------------------------+
  | The Original Code is: Elastix Open Source.                           |
  | The Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
  $Id: dialerd,v 1.2 2008/09/08 18:29:36 alex Exp $ */

if (count($argv) < 2) {
    fprintf(STDERR, "$argv[0] n.n.n.n/m\n");
	exit(1);
}
$sNetMask = $argv[1];
if (!preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$|', $sNetMask)) {
    fprintf(STDERR, "$argv[0]: invalid netmask\n");
	exit(1);
}
$retval = NULL;
exec("/usr/bin/nmap -sP -n  $sNetMask", $output, $retval);
$sIP = NULL;
foreach ($output as $linea) {
    $regs = NULL;
	if (preg_match('/^Host ([\.|[:digit:]]*) /', $linea, $regs))
        $sIP = $regs[1];
    if (preg_match('/^MAC Address: ([:|[:alnum:]]*) \((.*)\)/', $linea, $regs)) {
    	print "$regs[1] $sIP $regs[2]\n";
    }
}
exit($retval);
?>