0

I have some client inside an internal network but DHCP changes each time their IP.

I know that each client answer with a number on a REST call:

http://myclient/status ---> if it is only a number it is OK

I don't want to use domains because of some buggy behaviour that I don't have with IP address, so my solution is something like:

for (var i = 0; i < 255; i++) {

    var options = {
        host: '192.168.1.'+i,
        port: 80,
        path: '/status/',
        auth: 'root:password'
    };

    var request = http.get(options, function(htres){

        var __CLIENT_IP__ = ???? // how to access client ip here?

        var body = "";
        htres.on('data', function(data) {
            body += data;
        });
        htres.on('end', function() {
            if( htres.statusCode == 404 ) {
                res.end();
                return;
            }
            // ...
            // PARSE MY INT HERE
            // ...
        })
        htres.on('error', function(e) {
            console.log(e.message);
        });
    }).on('error', function(e) {
        var __CLIENT_IP__ = ???? // how to access client ip here?
        console.log(__CLIENT_IP__ + ' does not seems to have port 80 opened');

    });
};
nkint
  • 11,513
  • 31
  • 103
  • 174

2 Answers2

1

You can get the IP with htres.socket.remoteAddress.

If you really need it for the error handler you could do instead:

for (var i = 0; i < 255; i++) {

    var options = {
        host: '192.168.1.'+i,
        port: 80,
        path: '/status/',
        auth: 'root:password'
    };

    (function(__CLIENT_IP__) {
      var request = http.get(options, function(htres) {
          var body = "";
          htres.on('data', function(data) {
              body += data;
          });
          htres.on('end', function() {
              if( htres.statusCode == 404 ) {
                  res.end();
                  return;
              }
              // ...
              // PARSE MY INT HERE
              // ...
          })
          htres.on('error', function(e) {
              console.log(e.message);
          });
      }).on('error', function(e) {
          console.log(__CLIENT_IP__ + ' does not seems to have port 80 opened');
      });
    })(options.host);
};
mscdex
  • 104,356
  • 15
  • 192
  • 153
0

It may not be the best way to do this (especially in your case since you iterate 255 times), but here is a way to access it:

function request_all(i, options, callback) {
    if(i < 255)
    {
        var options = {
            host: '192.168.1.'+i,
            port: 80,
            path: '/status/',
            auth: 'root:password'
        };

        var request = http.get(options, function(htres){

            var __CLIENT_IP__ = options.host;
            //REST OF YOUR CODE HERE
        });

    request_all(i + 1, options, callback);
    }
}

EDIT: The duplicate answer is better by separating incrementation and execution... (But use JQuery)

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94