22 lines
671 B
TypeScript
22 lines
671 B
TypeScript
import { networkInterfaces } from "os";
|
|
|
|
export default function getMachineIPAddress() {
|
|
try {
|
|
const interfaces = networkInterfaces();
|
|
for (const ifaceName in interfaces) {
|
|
const iface = interfaces[ifaceName];
|
|
if (Array.isArray(iface)) {
|
|
for (const address of iface) {
|
|
if (address.family === "IPv4" && !address.internal) {
|
|
return address.address;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
} catch (error: any) {
|
|
console.error(`Error accessing network interfaces: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|