Getting IP LAN for sending with ajax

0

I need to get the "LOCAL" (LAN) ip, using javascript. It turns out that I have an application on my servers, but a client requested a customized service. I need to retrieve the client's local ip over the internet and send it to the database on my servers with ajax. I need to know how I could do with ActiveX or Java to get the information locally and put in a javascript variable so that I can make an AJAX call to load the information.

  

Example: Local IP: 192.168.1.1 | IP Wan: 182.236.10.25

The ip wan is easy to recover, I need Local IP (Lan).

    
asked by anonymous 05.11.2014 / 20:05

3 answers

1

I think you can not do this, because local addressing is a way for your router to distribute 1 WAN address to multiple machines on the LAN through NAT. The local address is managed by the router and is only accessible by those at the same level of the network. All machines on a LAN that communicate with another machine through the WAN will be communicating with the router's IP WAN.

    
05.11.2014 / 20:36
0

This function returns the IP of the machine that is accessing the page:

function myIP() {
  if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
  else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

  xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false);
  xmlhttp.send();

  hostipInfo = xmlhttp.responseText.split("\n");

  for (i = 0; hostipInfo.length >= i; i++) {
    ipAddress = hostipInfo[i].split(":");
    if (ipAddress[0] == "IP") return ipAddress[1];
  }

  return false;

}

var IP = myIP();
if (IP != false) {
  //alert(IP);
  /* Executa sua requisicao AJAX aqui. O IP estará armazenado na variavel IP */
};
    
05.11.2014 / 20:17
0

What I saw your application want to get its own local ip via JAVA?

public static String pegaIpLocal()
    {
        String s="";
        try
        {
            InetAddress in = InetAddress.getLocalHost();
            s = in.getHostAddress();

        } catch(Exception j){}

        return s;
    }

I have a solution with NODEJS, where you could already do this with javascript and already insert into the bank. Follow the code below, if you really want to implement this in nodejs let me know that I will help you. It's quiet.

var os=require('os'),
    mysql = require('mysql');

var ifaces=os.networkInterfaces();
var ip = [];
for (var dev in ifaces) {
  ifaces[dev].forEach(function(details){
    if (details.family=='IPv4') {
      ip.push(details.address);
    }
  });
}

var post = {
    ip : ip[0];
} 

var connection = mysql.createConnection({
  host     : 'ipDoServidor',
  user     : 'usuario',
  password : 'senha',
});

connection.connect();
connection.query('INSERT INTO tabela SET ?', post, function(err, result) {
    if(err){
        console.log('Erro ao inserir ip');
    }
    console.log('Ip inserido com sucesso');
});
    
05.11.2014 / 23:19