You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.2 KiB

import fs from 'fs';
import path from 'path';
export function parseLeases() {
const leaseFilePath = '/var/lib/dhcp/dhcpd.leases';
const fileContent = fs.readFileSync(leaseFilePath, 'utf8');
const leases = [];
const leaseBlocks = fileContent.split('lease ');
leaseBlocks.shift(); // Remove the first empty block
leaseBlocks.forEach(block => {
const lease = {};
const lines = block.split('\n');
lease.ip = lines[0].trim();
lines.forEach(line => {
line = line.trim();
if (line.startsWith('starts')) {
lease.start = line.split(' ')[2] + ' ' + line.split(' ')[3];
} else if (line.startsWith('ends')) {
lease.end = line.split(' ')[2] + ' ' + line.split(' ')[3];
} else if (line.startsWith('binding state')) {
lease.state = line.split(' ')[2];
} else if (line.startsWith('hardware ethernet')) {
lease.mac = line.split(' ')[2].replace(';', '');
} else if (line.startsWith('client-hostname')) {
lease.hostname = line.split(' ')[1].replace(';', '').replace(/"/g, '');
}
});
leases.push(lease);
});
return leases;
}

Powered by TurnKey Linux.