I'm running an Electron app on Windows and I need to determine if the current connection is wired or wireless.
I've found a solution for Linux systems here
tail -n+3 /proc/net/wireless | grep -q .
But I can't find anything that would work on Windows.
Ok I think I found out how to do it.
Using the command:
wmic nicconfig get Description,IPAddress,IPEnabled /format:csv
It is possible to determine which kind of connection you're in by iterating through the interface list and filtering them based on the "description".
Currently it looks for: Ethernet, Wi-Fi and Tethering (Wi-Fi through USB). I think there might other connection types, it would need to be included in the search.
The function returns the "best" connection, which is probably the one that Windows will choose.
Full code:
// returns: ethernet || wi-fi || tethering (wi-fi through usb)
async function tryGetConnectionType() {
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const command = `wmic nicconfig get Description,IPAddress,IPEnabled /format:csv`;
const response = (await exec(command)).stdout;
const interfaces = response.split("\r\r\n").slice(2).map(i => i.split(",")).map(i => ({
Description: i[1],
IPAddress: i[2] ? i[2].replace("{", "").replace("}", "").split(";") : [],
IPEnabled: i[3] === "TRUE",
}));
const parsed = interfaces.filter(i => i.IPEnabled === true).map(i => {
// determine if its tethering
if (i.Description.toLowerCase().includes("remote ndis")) {
i.type = "tethering";
return i;
}
// determine if its wi-fi
if (i.Description.toLowerCase().includes("wi-fi")) {
i.type = "wi-fi";
return i;
}
// determine if its ethernet
if (i.Description.toLowerCase().includes("ethernet")) {
i.type = "ethernet";
return i;
}
});
// return the "best" one
if (parsed.some(i => i.type === "ethernet")) {
return "ethernet";
} else if (parsed.some(i => i.type === "wi-fi")) {
return "wi-fi";
} else if (parsed.some(i => i.type === "tethering")) {
return "tethering";
} else {
return null;
}
}