Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Device/ Computer Discovery in my Network

Tags:

node.js

I was trying to write a sample program for my test project to find out all the devices(like android or IOS) or other computers connected to the network to which my computer is connected. I am able to see all the connected devices when I login to the router administration console and I want the same list using my program. I tried the sample code below which I came across the post at https://gist.github.com/chrishulbert/895382 and found it interesting and tried to use it, But i was not able to get the list. Am I missing something in the below code or this is a wrong sample I am referring to?. Any help would be greatly appreciated in this regard.

function listen(port) {
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) 
    {
  console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
});
server.bind(port); 

}

function search() {
var message = new Buffer(
    "M-SEARCH * HTTP/1.1\r\n" +
    "HOST:239.255.255.250:1900\r\n" +
    "MAN:\"ssdp:discover\"\r\n" +
    "ST:ssdp:all\r\n" + 
    "MX:3\r\n" + 
    "\r\n"
);

var client = dgram.createSocket("udp4");
client.bind(0,"",function() {
    console.log(client.address().port);
    listen(client.address().port);
    client.send(message, 0, message.length, 1900, "239.255.255.250",
                function()     {
        // client.close();
    });
}); // So that we get a port so we can listen before sending

}
search();
like image 206
Algi Avatar asked Jun 26 '14 14:06

Algi


People also ask

What is device discovery in networking?

Network discovery is a process that helps to map and monitor your network infrastructure. All the network devices can connect and communicate with each other.

Should my network discovery be on or off?

If you're connected to a network in a public location and you decide to turn on network discovery but leave network sharing turned off, the network discovery setting will be on for every public network you connect to from then on. This wouldn't be safe. That's why we recommend using the network sharing setting instead.

How do I see other computers on my network?

Open File Explorer on Windows 10. Click on Network from the left pane. See computers available in the local network. Double-click the device to access its shared resources, such as shared folders or shared printers.


2 Answers

@Algi, Your best bet for device discovery is to use the ICMP protocol which requires elevated privileges. Using the UDP protocol (which operates at OSI layer's 3 & 4, is not well suited for device discovery unless an existing client/server protocol for discovery is implemented such as those in use with DNS, NetBIOS & DropBox applications).

Please don't misunderstand that device discovery can be implemented on these higher level protocols but to assume a device doesn't exist on a network because UDP/TCP port N is not open is folly.

As @Josh3736 mentioned, SSDP can be implemented but because of its use of UPnP I would recommend against it for the reasons outlined in that article.

@Illizian, I am the author of the node-libnmap package and was wondering if you could elaborate when you say it is "unreliable". What version were you using? The latest revision stands at 0.1.10 and is quite stable.

Because it interfaces with the nmap binary, perhaps your scan results were being affected by --min-rt-timeout, --max-rt-timeout & --initial-rt-timeout options which implement the "Idle scan implementation algorithms" component(s).

Those components will set timeouts dynamically based on previous probe times which if scanning a heavily filtered (host based & perimeter based IDS & IPS systems) then you will most assuredly get unexpected results.

That being said, if you are experiencing problems outside of that range perhaps you have found a bug? If so could you please report it at https://github.com/jas-/node-libnmap/issues?

On a side note using the arp table to discover nearby hosts will not provide "all" available hosts on your network segment. Only those that are "chatty" at the time. The arp table is constantly pushing/popping machines off the table.

like image 114
jas- Avatar answered Oct 15 '22 12:10

jas-


I use nmap on a Unix system to get a list of devices on a network. There exists a NodeJS library for interacting with nmap (npm link | github); so you should be able to get a list of IPs using the following code:

require('node-libnmap').nmap('discover', function(err, report){
    if (err) throw err
    console.log(report)
});

and you will see the following output:

{
    adapter: 'eth0',
    properties: {
        address: '10.0.2.15',
        netmask: '255.255.255.0',
        family: 'IPv4',
        mac: '52:54:00:12:34:56',
        internal: false,
        cidr: '10.0.2.0/24',
        hosts: 256,
        range: { start: '10.0.2.1', end: '10.0.2.254' }
    },
    neighbors: [ '10.0.2.2', '10.0.2.3', '10.0.2.15' ]
}

Hope that helps :)

UPDATE: I found nmap a little unreliable and found an alternative in the node-arp library, the following snippet outputs an array of IPs and Mac Addresses parsed from the /proc/net/arp file:

var fs = require('fs');

fs.readFile('/proc/net/arp', function(err, data) {
    if (!!err) return done(err, null);

    var output = [];
    var devices = data.toString().split('\n');
    devices.splice(0,1);

    for (i = 0; i < devices.length; i++) {
        var cols = devices[i].replace(/ [ ]*/g, ' ').split(' ');

        if ((cols.length > 3) && (cols[0].length !== 0) && (cols[3].length !== 0) && cols[3] !== '00:00:00:00:00:00') {
            output.push({
                ip: cols[0],
                mac: cols[3]
            });
        }
    }

    console.log(output);
});
like image 35
Illizian Avatar answered Oct 15 '22 11:10

Illizian