Skip to content

Commit 57b4773

Browse files
committed
rewrote the parsing for the dhcpd leases
1 parent 96bc0b5 commit 57b4773

1 file changed

Lines changed: 8 additions & 28 deletions

File tree

bin/update_ipmi.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,18 @@ function update_ipmi_ip()
1818
die('You\'re not authorized');
1919
}
2020
$final = [];
21-
if ($stream = fopen('http://162.250.127.210/dhcpd.leases', 'rb')) {
22-
stream_get_contents($stream, true);
23-
$final = [];
24-
while ($line = fgets($stream)) {
25-
flush();
26-
if (strpos($line, 'lease ') !== false) {
27-
$temp_lease = substr($line, 6);
28-
$ip = substr(trim($temp_lease), 0, -2);
29-
}
30-
if (strpos($line, 'hardware ethernet ') !== false) {
31-
$temp_mac = substr($line, 20);
32-
$mac = substr(trim($temp_mac), 0, -1);
33-
$final[$mac] = $ip;
34-
}
21+
if (preg_match_all('/^lease ([\d\.]*) {.*hardware ethernet ([^;\n]*);?\n.*}/msuU', file_get_contents('http://162.250.127.210/dhcpd.leases'), $matches)) {
22+
foreach ($matches[1] as $idx => $ip) {
23+
$final[$matches[2][$idx]] = $ip;
3524
}
3625
}
37-
if ($stream = fopen('http://216.219.95.21/dhcpd.leases', 'rb')) {
38-
stream_get_contents($stream, true);
39-
$final = [];
40-
while ($line = fgets($stream)) {
41-
flush();
42-
if (strpos($line, 'lease ') !== false) {
43-
$temp_lease = substr($line, 6);
44-
$ip = substr(trim($temp_lease), 0, -2);
45-
}
46-
if (strpos($line, 'hardware ethernet ') !== false) {
47-
$temp_mac = substr($line, 20);
48-
$mac = substr(trim($temp_mac), 0, -1);
49-
$final[$mac] = $ip;
50-
}
26+
if (preg_match_all('/^lease ([\d\.]*) {.*hardware ethernet ([^;\n]*);?\n.*}/msuU', file_get_contents('http://216.219.95.21/dhcpd.leases'), $matches)) {
27+
foreach ($matches[1] as $idx => $ip) {
28+
$final[$matches[2][$idx]] = $ip;
5129
}
5230
}
31+
//myadmin_log('myadmin', 'debug', json_encode($final), __LINE__, __FILE__);
32+
//myadmin_log('myadmin', 'debug', json_encode($final[strtolower($GLOBALS['tf']->variables->request['ipmi_mac'])]), __LINE__, __FILE__);
5333
if (count($final) > 0) {
5434
$db = clone $GLOBALS['tf']->db;
5535
if (isset($_SERVER['SSH_CLIENT'])) {

0 commit comments

Comments
 (0)