Files
ontime/apps/server/src/utils/networkInterfaces.ts
2024-06-16 09:07:01 +02:00

29 lines
738 B
TypeScript

import { networkInterfaces } from 'os';
/**
* @description Gets information on IPV4 non-internal interfaces
* @returns {array} - Array of objects {name: ip}
*/
export function getNetworkInterfaces(): { name: string; address: string }[] {
const nets = networkInterfaces();
const results: { name: string; address: string }[] = [];
for (const name of Object.keys(nets)) {
const netObjects = nets[name];
if (!netObjects) {
continue;
}
for (const net of netObjects) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
results.push({
name,
address: net.address,
});
}
}
}
return results;
}