I am planning to write a small utility that will send an email to a predefined address whenever an IP address change takes place. I am planning to run the program as a monitoring tool at all times and it will check on the IP every so often. I think you get the idea.
Now, when I use the getLocalHost (along with getHostAddress) method of the InetAddress class under the java.net package I can get the IP address of the local machine I am working on. However, I am connected to the Internet through a router and I would like to learn the IP address that I am broadcasting to the INTERNET. I really don't care about my 192.168.2.3 address .
If anybody has any API suggestions I would be very happy. I would also welcome any suggestions regarding to the design of the program.
[ March 09, 2005: Message edited by: Ryan Madsen ] [ March 09, 2005: Message edited by: Ryan Madsen ]
Thats an interesting problem. Does ping, traceroute have any options that can display the WAN IP address along with other information?? There must be some utility which should be able to pick this up. *nix folks anyone??
But I can think of a crazy solution. Form a http request to www.whatismyip.com - get the html response and search for "Your ip is" - and the next string is your ip address. Easy for me to say Tell us what you discovered..
Originally posted by Venkatraman Kandaswamy: But I can think of a crazy solution. Form a http request to www.whatismyip.com - get the html response and search for "Your ip is" - and the next string is your ip address. Easy for me to say Tell us what you discovered..
That is very funny. I have also thought about the same thing. But, I will try that as a last resort. After all this a home-hobby project for me for the sake of learning some more java APIs.
Do you know the host name of your machine? If you do, you can get the IP address of it that is visible to the outside world.
Below is a sample code I've used. I am getting "192.168.2.4" as my ip address. This is my ip address within my home LAN. I want to learn the one that my machines broadcast to the internet. As a said my machines are connected with a belkin router. It is a simple home network.
1. Get the info from one of the what-is-my-ip-address type websites
2. Get the info from the admin interface of your router (if you are lucky, you won't have to log in).
3. Write a tiny cgi script/whatever to do the same as what the what-is-my-ip-address type websites do, but host it somewhere you have control over. Then see (1).
The problem is really that your workstation only knows the default gateway to use (your router). Your router then does all of the address translation stuff transparently, not giving you a chance to get the external address locally.
There may be some other way, but if there is, I don't know it.
If your router admin interface exposes your external IP address without having to log in, then you could use this code directly to query the info from there. If not, then I'd suggest using a list of services that provide your IP, an rotating through them. This would be nice to the service providers, and also allow you to try somewhere else if a particular server is down.
The only other thing that might be possible is if your router supports SNMP, you could do something with that, but I think this approach is far easier. [ March 09, 2005: Message edited by: Horatio Westock ]
I believe that if you are behind a router you do not have an external IP address (unless your system is set up as the DMZ host). You would have to find the IP of the router. Maybe check with the router manufacture to see if there is an HTTP request that will work for you.
Joined: Mar 08, 2005
Horatio's code solved the problem. I will add some more code just in case whatismyip.com 's server is down
Probably you can write an ipteller that just waits for requests and replies just the ip address of the request.Run this ipteller on a server elsewhere. Talk to ipteller to know your ip address. So we need not depend on any external entities.
Joined: Mar 08, 2005
here is what I am planning to implement: 1. use simple php code to learn my ip, run it on a place on the internet
It should be as simple as this
2. Using the sample code Heratio provided, I will get the ip address from my own web site. 3. Write this on a file 4. Anytime that there is a change of IP address, email the new ip to the predefined address.
Although I am familiar with OOP I am not very familiar with the JAVA APIs so I will definitely be back with more questions.