Skip to content

Commit 212c9ae

Browse files
committed
updated the update_ipmi script/function to pick the lease for each mac with the latest date
1 parent b138cc5 commit 212c9ae

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

bin/update_ipmi.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ function update_ipmi_ip()
1818
die('You\'re not authorized');
1919
}
2020
$final = [];
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;
24-
}
25-
}
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;
21+
foreach (['162.250.127.210', '216.219.95.21'] as $dhcpHost) {
22+
if (preg_match_all('/^lease (?P<ip>[\d\.]*) {.*ends (?P<dayofweek>\d+) (?P<year>\d+)\/(?P<month>\d+)\/(?P<day>\d+) (?P<hour>\d+):(?P<minute>\d+):(?P<second>\d+);.* ethernet (?P<mac>[a-f\d:]+)\s*;.*}/msuU', file_get_contents('http://'.$dhcpHost.'/dhcpd.leases'), $matches)) {
23+
foreach ($matches['mac'] as $idx => $mac) {
24+
// Parse the lease timestamp
25+
$timestamp = mktime($matches['hour'][$idx], $matches['minute'][$idx], $matches['second'][$idx], $matches['month'][$idx], $matches['day'][$idx], $matches['year'][$idx]);
26+
// Check if this MAC address is already in the final array
27+
if (!isset($final[$mac]) || $final[$mac]['timestamp'] < $timestamp) {
28+
// Store the IP and timestamp for the most recent lease
29+
$final[$mac] = [
30+
'ip' => $matches['ip'][$idx],
31+
'timestamp' => $timestamp
32+
];
33+
}
34+
}
2935
}
3036
}
3137
//myadmin_log('myadmin', 'debug', json_encode($final), __LINE__, __FILE__);
@@ -34,7 +40,8 @@ function update_ipmi_ip()
3440
$db = clone $GLOBALS['tf']->db;
3541
if (isset($_SERVER['SSH_CLIENT'])) {
3642
$update = 0;
37-
foreach ($final as $mac_key => $ip_val) {
43+
foreach ($final as $mac_key => $ipData) {
44+
$ip_val = $ipData['ip'];
3845
$db->query("SELECT * FROM assets WHERE ipmi_mac = '{$mac_key}' ORDER BY id DESC LIMIT 1");
3946
if ($db->num_rows() > 0) {
4047
$db->next_record(MYSQL_ASSOC);
@@ -45,7 +52,7 @@ function update_ipmi_ip()
4552
}
4653
echo $update.' row(s) updated.'.PHP_EOL;
4754
} elseif (isset($GLOBALS['tf']->variables->request['ipmi_mac'])) {
48-
$ip_val = $final[strtolower($GLOBALS['tf']->variables->request['ipmi_mac'])] ?? '';
55+
$ip_val = $final[strtolower($GLOBALS['tf']->variables->request['ipmi_mac'])]['ip'] ?? '';
4956
$mac_key = $GLOBALS['tf']->variables->request['ipmi_mac'];
5057
if ($mac_key) {
5158
$db->query("SELECT * FROM assets WHERE ipmi_mac = '{$mac_key}' ORDER BY id DESC LIMIT 1");

0 commit comments

Comments
 (0)