How to get IP address in Javascript?

R

Rex

Hi all,

I need to get the IP address of client machines
from inside Mozilla, Netscape, and IE.

Many webpages say to use in Netscape 4+
the expression

var ip = java.net.getLocalhost.getIpAddress()

This for me always returns 127.0.0.1.

Is this a bug or should I get trying to retrieve
an array of addresses and then scan that?

Thanks.
 
E

Evertjan.

Rex wrote on 30 jun 2006 in comp.lang.javascript:
Hi all,

I need to get the IP address of client machines
from inside Mozilla, Netscape, and IE.

Many webpages say to use in Netscape 4+
the expression

var ip = java.net.getLocalhost.getIpAddress()

That is java, not javascript
This for me always returns 127.0.0.1.

·. and indeed that is usually the ip of the localhost.
Is this a bug or should I get trying to retrieve
an array of addresses and then scan that?

The client IP adress is only available to the server
and to serverside code, I think,
and not to the client and it's code.

So using serverside j[ava]script under ASP it is simple:

<%
response.write(request.servervariables("remote_addr"));
%>

But then, the IP address is sometimes/usually not the machine address,
but the nat-router or the proxy address.
 
A

Aaron Gray

var ip = java.net.getLocalhost.getIpAddress()

This may do the trick in Java :-

String rhost = getDocumentBase().getHost();
Int rport = getDocumentBase().getPort();

String lhost = (new Socket(rhost,
rport)).getLocalAddress().getHostName();
String laddr = (new Socket(rhost,
rport)).getLocalAddress().getHostAddress();

Curtasy of some MetaSploit code.

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top