Feature #403 » dhcpd-leases.php
1 |
<?php
|
---|---|
2 |
function parse_dhcpd_leases($path) { |
3 |
$keys = array("starts", "ends", "binding state", "client-hostname"); |
4 |
|
5 |
$leases = array(); |
6 |
$fd = fopen($path, "r"); |
7 |
$entry = NULL; |
8 |
|
9 |
while(!feof($fd)) { |
10 |
# ignore empty lines and comments
|
11 |
list($line, $ignored) = explode("#", $line, 1); |
12 |
$line = trim(fgets($fd)); |
13 |
if (strlen($line) == 0) { |
14 |
continue; |
15 |
}
|
16 |
|
17 |
if (1 == sscanf($line, "lease %s {", $ip)) { |
18 |
$leases[$ip] = array(); |
19 |
$entry = &$leases[$ip]; |
20 |
continue; |
21 |
}
|
22 |
if ($line == "}") { |
23 |
unset($entry); |
24 |
continue; |
25 |
}
|
26 |
foreach ($keys as $key) { |
27 |
if (1 == sscanf($line, $key . " %[^;];", $val)) { |
28 |
$entry[$key] = trim($val, '"'); |
29 |
break; |
30 |
}
|
31 |
}
|
32 |
}
|
33 |
return $leases; |
34 |
}
|