Rajeev
Rajeev

Reputation: 193

IP Address: using program or script

A) The below statement

System.out.println(java.net.Inet4Address.getLocalHost());

shows output as myhostname/192.168.1.25

B) The http://whatismyipaddress.com/ shows below (masked):

14x.7x.2xx.x1x

Using above mentioned technique A (any language or script), how do I get the value 14x.7x.2xx.x1x that above mentioned technique B shows? Or, is it at all possible?

In other words (?), using technique A (as mentioned in original question), how I can I get the same public Ip address of my home router which is shown by technique B? If it is impossible, please explain briefly (two lines at the most) why?

Thanks

Upvotes: 0

Views: 86

Answers (2)

Shenal
Shenal

Reputation: 202

Refer to this question:Getting the 'external' IP address in Java

The computer don't know its final public address as it is not necessary for it to know. Local network IP Address is sufficient for it for the tasks. But if you want to get the public address you have to use an external service as mentions in the answers of the linked question.

Upvotes: 1

M. K. Hunter
M. K. Hunter

Reputation: 1828

I will assume that you are at your home and you have a router for connecting your several computers to the internet.

http://whatismyipaddress.com/ will show an IP address as it is visible on the global internet. This may well be the IP address of your in-home router, which exposes one address for your entire in-home network. This is your public IP address.

The script that you show will give you the IP address of your own machine, which is usually only useful on your own local network. This is a private IP address, only accessible from your local network.

Upvotes: 0

Related Questions