get_active_interface = function (cb) { os_functions.get_active_network_interface_name(function(err, nic_name) { if (err || !nic_name) return cb(err || new Error("No active interfaces detected.")); nic_by_name(nic_name, function(err, nic) { if (err) return cb(err); os_functions.netmask_for(nic_name, function(err, netmask) { if (!err && netmask) nic.netmask = netmask.trim(); os_functions.gateway_ip_for(nic_name, function(err, ip) { if (!err && ip) nic.gateway_ip = ip.toString().trim(); cb(null, nic); }) }); }); }); }
...
## Get active interface
Returns the IP, MAC address and interface type for the active network
interface. On OS X and Linux you also get the IP of its assigned gateway.
``` js
network.get_active_interface(function(err, obj) {
/* obj should be:
{ name: 'eth0',
ip_address: '10.0.1.3',
mac_address: '56:e5:f9:e4:38:1d',
type: 'Wired',
...
get_gateway_ip = function (cb) { os_functions.get_active_network_interface_name(function(err, nic_name) { if (err || nic_name.trim() == '') return cb(err || new Error('No active network interface found.')); os_functions.gateway_ip_for(nic_name, function(err, out) { if (err || !out || out.toString() == '') return cb(err || new Error('No gateway IP assigned to ' + nic_name)); cb(null, out.toString().trim()) }) }); }
...
$ network private_ip
## Get gateway IP
Returns the IP of the gateway that your active network interface is linked to.
``` js
network.get_gateway_ip(function(err, ip) {
console.log(err || ip); // err may be 'No active network interface found.'
})
```
##### CLI
$ network gateway_ip
...
get_interfaces_list = function (cb) { var count = 0, list = [], nics = os.networkInterfaces(); function append_data(obj) { async.parallel([ function(cb) { exports.mac_address_for(obj.name, cb) }, function(cb) { exports.gateway_ip_for(obj.name, cb) }, function(cb) { exports.netmask_for(obj.name, cb) }, function(cb) { exports.interface_type_for(obj.name, cb) } ], function(err, results) { if (results[0]) obj.mac_address = results[0]; if (results[1]) obj.gateway_ip = results[1]; if (results[2]) obj.netmask = results[2]; if (results[3]) obj.type = results[3]; list.push(obj); --count || cb(null, list); }) } for (var key in nics) { if (key != 'lo0' && key != 'lo' && !key.match(/^tun/)) { count++; var obj = { name: key }; nics[key].forEach(function(type) { if (type.family == 'IPv4') { obj.ip_address = type.address; } }); append_data(obj); } } if (count == 0) cb(new Error('No interfaces found.')) }
...
## Get interfaces list
Returns list of network interfaces, including MAC addresses and the such, just
as in the example above.
``` js
network.get_interfaces_list(function(err, list) {
/* list should be:
[{
name: 'eth0',
ip_address: '10.0.1.3',
mac_address: '56:e5:f9:e4:38:1d',
...
get_private_ip = function (cb) { os_functions.get_network_interfaces_list(function(err, list) { if (err || !list) return cb(err || new Error('No network interfaces found.')); os_functions.get_active_network_interface_name(function(err, active_nic) { if (err) return cb(err); var ips = list.filter(function(nic) { if (is_ip_address(nic.ip_address)) return active_nic ? active_nic == nic.name : true; }); if (ips.length > 0) cb(null, ips[0].ip_address); else cb(new Error('No private IPs found (' + list.length + ' interfaces)')); }); }); }
...
$ network public_ip
## Get private IP
Returns the IP address assigned to your first active network inteface.
``` js
network.get_private_ip(function(err, ip) {
console.log(err || ip); // err may be 'No active network interface found'.
})
```
##### CLI
$ network private_ip
...
get_public_ip = function (options, cb) { var default_urls = [ 'checkip.dyndns.org', 'http://wtfismyip.com/text', 'http://ipecho.net/plain', 'http://ifconfig.me/ip' ]; if (typeof options == 'function') { // no options passed cb = options; options = {}; } var urls = options.urls || default_urls; function get(i) { var url = urls[i]; if (!url) return cb(new Error('Unable to fetch IP address.')); needle.get(url, function(err, resp) { var body = resp && resp.body.toString(); if (body && body.match(ip_regex)) { return cb(null, body.match(ip_regex)[1]); } get(i+1); }) }; get(0); }
...
## Get public IP
Returns your public IP address, as reported by DynDNS.org or other services.
``` js
var network = require('network');
network.get_public_ip(function(err, ip) {
console.log(err || ip); // should return your public IP address
})
```
##### CLI
$ network public_ip
...
mac_address_for = function (nic_name, cb) { var cmd = 'cat /sys/class/net/' + nic_name + '/address'; trim_exec(cmd, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.mac_address_for(obj.name, cb)
},
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
},
...
gateway_ip_for = function (nic_name, cb) { var cmd = "ipconfig getoption " + nic_name + " router"; trim_exec(cmd, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
...
get_active_network_interface_name = function (cb) { var cmd = "netstat -rn | grep UG | awk '{print $6}'"; exec(cmd, function(err, stdout) { if (err) return cb(err); var raw = stdout.toString().trim().split('\n'); if (raw.length === 0 || raw === ['']) return cb(new Error('No active network interface found.')); cb(null, raw[0]); }); }
n/a
get_network_interfaces_list = function (cb) { var count = 0, list = [], nics = os.networkInterfaces(); function append_data(obj) { async.parallel([ function(cb) { exports.gateway_ip_for(obj.name, cb) }, function(cb) { exports.netmask_for(obj.name, cb) } ], function(err, results) { if (results[0]) obj.gateway_ip = results[0]; if (results[1]) obj.netmask = results[1]; list.push(obj); --count || cb(null, list); }) } exec('networksetup -listallhardwareports', function(err, out) { if (err) return cb(err); var blocks = out.toString().split(/Hardware/).slice(1); count = blocks.length; blocks.forEach(function(block) { var parts = block.match(/Port: (.+)/), mac = block.match(/Address: ([A-Fa-f0-9:-]+)/), name = block.match(/Device: (\w+)/); if (!parts || !mac || !name) return --count; var obj = {}, port = parts[1]; obj.name = name[1]; // obj.desc = port; obj.type = determine_nic_type(port); obj.ip_address = null; obj.mac_address = mac[1]; (nics[obj.name] || []).forEach(function(type) { if (type.family == 'IPv4') { obj.ip_address = type.address; } }); append_data(obj); }) if (count == 0) cb(new Error('No interfaces found.')) }) }
n/a
mac_address_for = function (nic_name, cb) { var cmd = "networksetup -getmacaddress " + nic_name + " | awk '{print $3}'"; trim_exec(cmd, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.mac_address_for(obj.name, cb)
},
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
},
...
netmask_for = function (nic_name, cb) { var cmd = "ipconfig getoption " + nic_name + " subnet_mask"; trim_exec(cmd, cb); }
...
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
list.push(obj);
--count || cb(null, list);
...
gateway_ip_for = function (nic_name, cb) { trim_exec("ip r | grep " + nic_name + " | grep default | cut -d ' ' -f 3", cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
...
get_active_network_interface_name = function (cb) { var cmd = "netstat -rn | grep UG | awk '{print $NF}'"; exec(cmd, function(err, stdout) { if (err) return cb(err); var raw = stdout.toString().trim().split('\n'); if (raw.length === 0 || raw === ['']) return cb(new Error('No active network interface found.')); cb(null, raw[0]); }); }
n/a
get_network_interfaces_list = function (cb) { var count = 0, list = [], nics = os.networkInterfaces(); function append_data(obj) { async.parallel([ function(cb) { exports.mac_address_for(obj.name, cb) }, function(cb) { exports.gateway_ip_for(obj.name, cb) }, function(cb) { exports.netmask_for(obj.name, cb) }, function(cb) { exports.interface_type_for(obj.name, cb) } ], function(err, results) { if (results[0]) obj.mac_address = results[0]; if (results[1]) obj.gateway_ip = results[1]; if (results[2]) obj.netmask = results[2]; if (results[3]) obj.type = results[3]; list.push(obj); --count || cb(null, list); }) } for (var key in nics) { if (key != 'lo0' && key != 'lo' && !key.match(/^tun/)) { count++; var obj = { name: key }; nics[key].forEach(function(type) { if (type.family == 'IPv4') { obj.ip_address = type.address; } }); append_data(obj); } } if (count == 0) cb(new Error('No interfaces found.')) }
n/a
interface_type_for = function (nic_name, cb) { exec('cat /proc/net/wireless | grep ' + nic_name, function(err, out) { return cb(null, err ? 'Wired' : 'Wireless') }) }
...
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
},
function(cb) {
exports.interface_type_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.mac_address = results[0];
if (results[1]) obj.gateway_ip = results[1];
if (results[2]) obj.netmask = results[2];
if (results[3]) obj.type = results[3];
...
mac_address_for = function (nic_name, cb) { var cmd = 'cat /sys/class/net/' + nic_name + '/address'; trim_exec(cmd, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.mac_address_for(obj.name, cb)
},
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
},
...
netmask_for = function (nic_name, cb) { var cmd = "ifconfig " + nic_name + " 2> /dev/null | egrep 'netmask|Mask:' | awk '{print $4}' | sed 's/Mask://'"; trim_exec(cmd, cb); }
...
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
list.push(obj);
--count || cb(null, list);
...
gateway_ip_for = function (nic_name, cb) { get_wmic_ip_value('DefaultIPGateway', nic_name, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
...
get_active_network_interface_name = function (cb) { wmic.get_value('nic', 'NetConnectionID', 'NetConnectionStatus = 2', cb); }
n/a
get_network_interfaces_list = function (callback) { var list = [], node_nics = os.networkInterfaces(); wmic.get_list('nic', function(err, nics) { if (err) return callback(err); nics.forEach(function(nic){ if (nic.Name && nic.NetConnectionID != '' && nic.MACAddress != '') { var obj = { name: nic.NetConnectionID, // description: nic.Name, mac_address: nic.MACAddress, ip_address: nic.IPAddress, vendor: nic.Manufacturer, model: nic.Description, type: nic.Name.match(/wi-?fi|wireless/i) ? 'Wireless' : 'Wired' } var node_nic = node_nics[obj.name] || []; node_nic.forEach(function(type){ if (type.family == 'IPv4') { obj.ip_address = type.address; } }); list.push(obj); } }) callback(null, list); }); }
n/a
mac_address_for = function (nic_name, cb) { var cond = 'NetConnectionID = \'' + nic_name + '\''; wmic.get_value('nic', 'MACAddress', cond, cb); }
...
var count = 0,
list = [],
nics = os.networkInterfaces();
function append_data(obj) {
async.parallel([
function(cb) {
exports.mac_address_for(obj.name, cb)
},
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
},
...
netmask_for = function (nic_name, cb) { get_wmic_ip_value('IPSubnet', nic_name, cb); }
...
function append_data(obj) {
async.parallel([
function(cb) {
exports.gateway_ip_for(obj.name, cb)
},
function(cb) {
exports.netmask_for(obj.name, cb)
}
], function(err, results) {
if (results[0]) obj.gateway_ip = results[0];
if (results[1]) obj.netmask = results[1];
list.push(obj);
--count || cb(null, list);
...