4

What's the best way nowadays to find the default gateway IP Address in Node.js?

os.networkInterfaces() doesn't provide this info.

The only idea coming in mind is to parse the stdout of a subprocess route -n:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.2     0.0.0.0         UG    100    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
Rocco Musolino
  • 610
  • 10
  • 22
  • http://stackoverflow.com/questions/24378272/node-js-how-to-get-the-ip-address-of-http-server-listening-on-a-specific-port Possible Duplicate – Asif Saeed Jan 17 '17 at 11:05
  • @AsifSaeed It's not related at all. We're not talking about a server. The scenario it totally different. – Rocco Musolino Jan 17 '17 at 11:07

3 Answers3

3

Use network package : https://www.npmjs.com/package/network

;)

network.get_gateway_ip(function(err, ip) {
  console.log(err || ip); // err may be 'No active network interface found.' 
})
Mech45
  • 321
  • 1
  • 9
  • 1
    FWIW, this package essentially does what the OP asked. For example, to obtain the default gateway on a Linux system it analyzes the output of `ip route`: https://github.com/tomas/network/blob/master/lib/linux.js#L42 – jacobq Oct 10 '19 at 15:39
  • And it does so very poorly! The implementation uses: `trim_exec("ip r | grep " + nic_name + " | grep default | cut -d ' ' -f 3 | head -n1", cb);`. Parsing is done in a shell with a multitude of shell tools. Not to mention the potential security risk of feeding any string into the commands. IPv6 is not supported (`ip -6 route`). And on Linux you don't need to parse `ip`, you can just parse `/proc/net/route` which does not need any child process to be spawned at all. – Yeti Aug 02 '23 at 19:10
0

Since all the other ways don't work and since JavaScript is so limited. Here's my piece of code.. I am not really good at javas- but the logic is that if an IP responds in a specific amount of time, then it's the gateway IP. If not then it's not. The more gateway IPs are in the list, the better, however there are still downs of using this script. Since most gateway IPs are actually occupied by devices on other networks. This might cause problems and results may not be accurate.

var responses = [];
var ips = ["192.168.1.1", "192.168.0.1", "10.0.0.138", "192.168.2.1", "192.168.254.254", "10.0.1.1", "192.168.3.1", "10.10.1.1", "10.0.0.1", "10.0.0.2", "10.1.1.1", "192.168.11.1", "192.168.0.30", "192.168.0.50", "192.168.0.10", "192.168.0.101", "192.168.15.1", "10.90.90.90", "192.168.8.1", "192.168.86.1", "192.168.100.1", "192.168.123.254", "192.168.16.1", "192.168.10.1", "192.168.20.1", "192.168.30.1", "192.168.62.1", "192.168.102.1", "192.168.0.227", "192.168.10.50", "10.1.10.1", "192.168.0.3", "192.168.168.168", "192.168.50.1", "192.168.55.1", "192.168.251.1", "192.168.0.254", "192.168.0.100", "192.168.10.10", "192.168.10.100", "192.168.223.100", "200.200.200.5", "192.168.4.1", "192.168.100.100", "192.168.2.254"];
var length = ips.length

for (var i = 0; i < length; i++){
    (async function(){
        var connection = new WebSocket('ws://' + ips[i] + ':80');
        await new Promise (function(res){
                var timeout = setTimeout(function() {
                                console.log("Socket connection timeout", connection.readyState);
                                console.log(connection.url);
                                if (connection.readyState == 3){
                                    responses.push('valid')
                                    alert(connection.url);
                                } else {
                                    responses.push('invalid')
                                }
                                connection.close();
                }, 5000);
                res();
        });
    })();

}
  • "This might cause problems and results may not be accurate." is a fair statement. Using a "hack" like this to indirectly draw conclusions about the system state should only be used as a last resort, i.e., when no other methods are available. This answer seems to assume that a "gateway" is an HTTP server listening on port 80 that happens to have a common IP address and perhaps also that the code is running in a browser rather than Node.js as the OP stated. Consider using [`default-gateway`](https://www.npmjs.com/package/default-gateway) instead. – jacobq Nov 28 '22 at 21:40
-1

In Ubuntu I try like this

const spawn = require('child_process').spawnSync;

const child = spawn('bash', ['-c', 'ip r']).stdout.toString();
const gateway = child.match(/default via (.*?)\s/)[1];
console.log(gateway);
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39