Link to home
Start Free TrialLog in
Avatar of deve-lopez
deve-lopezFlag for United States of America

asked on

Retrieving local IP assigned from VPN DHCP

Hi,

My computer is connected to Internet via a Router.
The Router is connected to my ISP.
I connect to an Office WAN via a VPN connection.

Using Java code:
I need to retrieve the Dynamic IP address assigned to my computer by the VPN DHCP.

I tried to use the code sample bellow but it returns the LAN IP address.
My knowledge of the package java.net.* is limited.

Any help would be greatly appreciated.

Thanks
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.toString()); // returns 10.0.1.1 (the LAN IP assigned by the router)

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You could make a connection to whatismyip.com and read the response
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deve-lopez

ASKER

whatismyip returns the IP of the web gateway when accessed while on VPN.

I found the method to obtain all the IPs assigned.
I just have to pick the good one from the list.
Attached is the code for anyone who is interested.

Thanks
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            //InetAddress address = InetAddress.getLocalHost();
            //InetAddress address = InetAddress.getByName("lvignals-us64.us.oracle.com");
            InetAddress address[] = InetAddress.getAllByName("lvignals-us64.us.oracle.com");
            for (int i=0; i<address.length; i++) {
                System.out.println(address[i].toString());
            }
        } catch (UnknownHostException e) {
            System.out.println("LVI: Could not determine this computer address..."+e.toString());
        }
    }

Open in new window

> Actually http://www.whatismyip.com/automation/n09230945.asp

you don't need to do that, and it won't return what you want anyway
>             InetAddress address[] = InetAddress.getAllByName("lvignals-us64.us.oracle.com");

you don't need to know the hostname, see the code I posted above
>>whatismyip returns the IP of the web gateway when accessed while on VPN.

Yes, that's usually what you need
In this case what was needed was the IP of the specific laptop running the code when that laptop is connected via VPN:

InetAddress address[] = InetAddress.getAllByName("machinename.domain.com");

does exactly what is needed, it returns the list of IP used by each active network adapter on the laptop.

what I posted above should provide that, did you try it?
Thanks