Gibt ein Array heraus ($DhcpArray), welches die DHCP-Tabelle eines Lancom-Routers enthält.
Es müssen folgende Parameters gesetzt werden:
$router_ip = ‚string‘;
$username = ‚string‘;
$password = ‚string‘;
<?php
$port = 23;
$timeout = 10;
$router_ip = '';
$username = '';
$password = '';
$connection = fsockopen($router_ip, $port, $errno, $errstr, $timeout);
if(!$connection){
echo "Connection failed
";
exit();
} else {
echo "Connected
";
fputs($connection, "$username
");
fputs($connection, "$password
");
fputs($connection, "cd setup/dhcp/dhcp-table
");
fputs($connection, "dir
");
fputs($connection, " ");
$j = 0;
while ($j < 16) {
fgets($connection);
$j++;
}
stream_set_timeout($connection, 2);
$timeoutCount = 0;
$content ='';
$DhcpArray = '';
(int) $index =0;
$DhcpFile = "C:\IP-Symcon\webfront\user\images\LancomDhcp.txt";
$fh = fopen($DhcpFile, 'w') or die("can't open file");
while (!feof($connection)){
$content = fgets($connection);
$content = str_replace("\r", '', $content);
$content = str_replace("
", "", $content);
if (isValidIp(explode(' ', $content)[0]))
{ $index +=1;
$DhcpArray[$index]['WholeString']= $content;
$DhcpArray[$index]['IP-Address'] = substr ($content, 0,17);
$DhcpArray[$index]['MAC-Address'] = substr ($content, 17,32-18);
$DhcpArray[$index]['Timeout'] = substr ($content, 31,41-32);
$DhcpArray[$index]['Hostname'] = substr ($content, 40,108-41);
$DhcpArray[$index]['Type'] = substr ($content, 107,125-108);
$DhcpArray[$index]['LAN-Ifc'] = substr ($content, 124,137-125);
$DhcpArray[$index]['Ethernet-Port'] = substr ($content, 136,152-137);
$DhcpArray[$index]['VLAN-ID'] = substr ($content, 151,161-152);
$DhcpArray[$index]['Network-Name'] = substr ($content, 160);
fwrite($fh, $content);
//$DhcpArray[$index]= preg_split('/ +/', $content);} // each element is an array
}
# If the router say "press space for more", send space char:
if (preg_match('/MORE/', $content) ){ // IF current line contain --More-- expression,
fputs ($connection, " "); // sending space char for next part of output.
} # The "more" controlling part complated.
$info = stream_get_meta_data($connection);
if ($info['timed_out']) { // If timeout of connection info has got a value, the router not returning a output.
$timeoutCount++; // We want to count, how many times repeating.
}
if ($timeoutCount >2){ // If repeating more than 2 times,
break; // the connection terminating..
}
}
$content = substr($content,410);
print "content: ".chr(13).$content."
";
print_r ($DhcpArray);
fclose($fh);
}
echo "End.
";
function isValidIp($ip)
{/* PCRE Pattern written by Junaid Atari */
return !preg_match ( '/^([1-9]\d|1\d{0,2}|2[0-5]{2})\.('.
'(0|1?\d{0,2}|2[0-5]{2})\.){2}(0|1?'.
'\d{0,2}|2[0-5]{2})(\:\d{2,4})?$/',
(string) $ip )
? false
: true;
}
?>