Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

ip scanner node

var child = require("child_process"); 
var async = require("async"); 
var net = require("net"); 
var os = require("os"); 

function scan(port, cb){
    var hosts = {}; 
    var result = []; 
    async.series([
        function scan(next, c){
            if(c == 1){
                next(); return; 
            }
            // scan twice because arp sometimes does not list all hosts on first time
            child.exec("arp -n | awk '{print $1}' | tail -n+2", function(err, res){
                if(err) cb(err); 
                else {
                    var list = res.split("
").filter(function(x){return x !== "";}); 
                    list.map(function(x){
                        hosts[x] = x; 
                    }); 
                }
                scan(next, 1); 
            }); 
        },
        function(next){
            // if you want to scan local addresses as well 
            var ifs = os.networkInterfaces(); 
            Object.keys(ifs).map(function(x){
                hosts[((ifs[x][0])||{}).address] = true; 
            }); 
            // do the scan
            async.each(Object.keys(hosts), function(x, next){
                var s = new net.Socket(); 
                s.setTimeout(1500, function(){s.destroy(); next();}); 
                s.on("error", function(){
                    s.destroy(); 
                    next(); 
                }); 
                s.connect(port, x, function(){
                    result.push(x); 
                    s.destroy(); 
                    next(); 
                }); 
            }, function(){
                next();
            });
        }
    ], function(){
        cb(null, result); 
    }); 
} 

scan(80, function(err, hosts){
    if(err){
        console.error(err); 
    } else {
        console.log("Found hosts: "+hosts);
    } 
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: pass a react component as a prop from another component 
Javascript :: maptable elo 
Javascript :: Fibonacci numbers for n terms 
Javascript :: empty or remove div span class 
Javascript :: confirming the end javascript 
Javascript :: js get first elements of array 
Javascript :: open bootstrap modal from another modal 
Javascript :: como hacer un contador de tiempo en javascript 
Javascript :: Calculate sum of last column in dynamically added rows using javascript 
Javascript :: check if first array contains all elements javascript 
Javascript :: javascript call function change last default value 
Javascript :: Dependency Injection in Node.js 
Javascript :: sweet alert for react 
Javascript :: check for overlapping time javascript 
Javascript :: javascript protect object with proxy 
Javascript :: wait untill 2 
Javascript :: Prevent HTTP Parameter Polution in NodeJS 
Javascript :: Populate a Select Dropdown List using JSON 
Javascript :: how to add picture to picture video js in old library in js 
Javascript :: remove parent element jquery 
Javascript :: break and continue in javascript 
Javascript :: convert jquery to javascript converter online tool 
Javascript :: what is callback hell in javascript 
Javascript :: how to upload file in node js 
Javascript :: for-in loop 
Javascript :: Manage selection fabric js 
Javascript :: EFSavechanges 
Javascript :: javascript Convert to Number Explicitly 
Javascript :: javascript Arrow Function with Promises and Callbacks 
Javascript :: electron InitializeSandbox() called with multiple threads in process gpu-process. 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =