Picking up IP LAN for sending with ajax

Asked

Viewed 3,234 times

0

I need to get the ip "LOCAL" (LAN) using javascript. I happen to have an application on my servers, but a client requested a customized service. I need to retrieve the client’s local IP address over the Internet and send it to the database on my ajax servers. I need to know how I could do with Activex or Java to take the information locally and put it into a javascript variable so I can make an AJAX call to load the information.

Example: Local IP: 192.168.1.1 | IP Wan: 182.236.10.25

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

3 answers

1

I don’t think you can do that, because local addressing is a way for your router to distribute 1 WAN address to various machines on the LAN through NAT. The local address is managed by the router and is only accessible by those who are on the same network level. All machines of a LAN that communicates with another machine through the WAN, it will be communicating with the WAN IP of the router.

  • OK @Antony Alkmin, via pure javascript I know that there is no way. I am asking how to retrieve such information through an Activex or a Java that will run locally on the machine, where the information is accessible.

0

This function returns the IP of the machine 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 */
};

  • Ok, but this is to get the WAN ip. This I could do in PHP with $_SERVER['REMOTE_ADDR'];

  • The return should be "192.168.1.5" for example.

0

From what I’ve seen your application wants 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 in the database. Follow the code below, if you really want to implement this in nodejs let me know that I 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');
});
  • Vinicius that needs to run on the client’s machine inside a web page, with the nodejs installed on the client’s machine would work?

  • Employee yes, but maybe then you don’t even need to use the nodejs server. What are you using in your backend there? Because thus, the only way to take it to the "front" to send via ajax is to first make an ajax request in a backend file that returns the IP, so you already call another ajax to send to your database...

  • My backend is a PHP that will receive a request post via javascript with local ip.

  • For example, from here now I would need to know what the IP Lan of your machine there when you access a page of mine, for that it would be necessary that you do the installation of something in your machine that would pass me this information for javascript for me to send an ajax request. Got it?

  • I understood then. I will write the script and edit my answer. It will be like I said, we will have a PHP file that will return you the IP that I will request via ajax and then you take the ip and call another ajax for your php script. 5 minutes. OK

  • $_SERVER['REMOTE_ADDR'] will return me the WAN IP, remember...

  • There is understood, but friend for this you need to have php in the client machine, otherwise it can’t be done!! You need to have a host there where you access and take the IP. Or the client needs to have the fixed IP running a local script. Maybe socket would solve your problem, but still need to have the server on the other side, OK ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.