DHCP-Tabelle aus Lancom-Router

Falls da nochmal jemand drüber stolpert, das geht auch mit SNMP ohne viel herumbasteln mit der String-Formatierung oder nur halben Listen.

SNMP User im Lancom anlegen: SNMP-Einstellungen

Die SNMP Bibliothek GitHub - FreeDSx/SNMP: A Pure PHP SNMP Library. muss entsprechend im scripts/snmp Ordner liegen.

Zwei Beiträge weiter unten ist die lauffähige SNMP-Bibliothek inkl. autoload.php hochgeladen.

$HTMLBOXID = 12345; // HTML Box mit Ausgabe
require_once('.\snmp\autoload.php');
use FreeDSx\Snmp\SnmpClient;
$snmp = new SnmpClient([
   'host' => '192.168.123.1',
   'version' => 3,
   'user' => 'SYMCON',
   'use_auth' => true,
   'auth_mech' => 'sha1',
   'auth_pwd' => "SymconPWD",
   'use_priv' => true,
   'priv_mech' => 'aes128',
   'priv_pwd' => 'SymconPWD',
]);
$dhcptable = array();
$start = "1.3.6.1.4.1.2356.11.1.9.6.16";  
try {
  $walk= $snmp->walk($start,$start);
  while($walk->hasOids()) {
    try {
        $oid = $walk->next();
        //$ident = "_".str_replace(".","_",$oid->getOid());
        //$id = IPS_GetObjectIDByIdent($ident, $parent);
        $value = $oid->getValue()->getValue();
        $shortoid = str_replace(array('1.3.6.1.4.1.2356.11.1.9.6.16.1.' /*prefix*/, '.8.73.78.84.82.65.78.69.84' /*Suffix*/) ,'',$oid->getOid());
        $pos =  strpos($shortoid, '.');
        $type = substr($shortoid, 0, $pos);
        $ip = substr($shortoid, $pos+1);
        $dhcptable[$ip][$type]=$oid->getValue()->getValue();

    } catch (\Exception $e) {
        echo "Unable to retrieve OID. ".$e->getMessage().PHP_EOL;
    }
  } // ende while
} catch (\Exception $e) { /* catchblock zu ->walk*/ echo "Unable to walk. ".$e->getMessage().PHP_EOL;}
array_multisort (array_column($dhcptable, '11'), SORT_DESC, $dhcptable);
//print_r($dhcptable);

$out='<table class="wwx">';
foreach($dhcptable as $k => $v){
    $macad = (strlen($v[2])>12) ? $v[2] : join(':', str_split($v[2], 2));
    $macad = strtolower($macad);
    $out.='<tr><td><a target="_blank" href="https://'.$v[1].'">secure</a> - <a target="_blank" href="http://'.$v[1].'">'.$v[1].'</a></td><td>'.$macad.'</td><td>'.$v[4].'</td><td>'.$v[11].'</td></tr>';
 }
$out.='</table>';
SetValue($HTMLBOXID, $out);