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.

48 lines
1.5 KiB

import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
const leasesFilePath = '/var/lib/dhcp/dhcpd.leases';
function parseLeases(fileContent) {
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;
}
export async function GET() {
try {
const fileContent = fs.readFileSync(leasesFilePath, 'utf8');
const leases = parseLeases(fileContent);
return NextResponse.json({ leases });
} catch (error) {
return NextResponse.json({ error: 'Failed to read DHCP leases file' }, { status: 500 });
}
}

Powered by TurnKey Linux.