aspose file tools*
The moose likes Java in General and the fly likes Learn your IP number and email it?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Learn your IP number and email it??" Watch "Learn your IP number and email it??" New topic
Author

Learn your IP number and email it??

Ryan Madsen
Greenhorn

Joined: Mar 08, 2005
Posts: 8
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 ]
Venkatraman Kandaswamy
Ranch Hand

Joined: Jul 07, 2004
Posts: 120
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..


--Venkatraman<br />SCJP 1.4<br /><a href="http://kvrlogs.blogspot.com" target="_blank" rel="nofollow">blog</a>
Ryan Madsen
Greenhorn

Joined: Mar 08, 2005
Posts: 8
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.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Ryan,

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.

And here's how:



HTH


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ryan Madsen
Greenhorn

Joined: Mar 08, 2005
Posts: 8
Originally posted by Chengwei Lee:
Hi Ryan,

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.

Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
You have a few choices:

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.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Out of curiosity, I tried this.



Obviously, you've got to handle MalformedURLException and IOException yourself, but it works.

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 ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Ryan Madsen
Greenhorn

Joined: Mar 08, 2005
Posts: 8
Horatio's code solved the problem. I will add some more code just in case whatismyip.com 's server is down
Venkatraman Kandaswamy
Ranch Hand

Joined: Jul 07, 2004
Posts: 120
whatismyip.com sold for $386,000 on ebay

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.
Ryan Madsen
Greenhorn

Joined: Mar 08, 2005
Posts: 8
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.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
whatismyip.com sold for $386,000 on ebay


Great return for 10 minutes worth of work!

Geoffrey


Sun Certified Programmer for the Java 2 Platform
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

On GNU/ Linux I get my inet-adress by:


On windows it will be 'ipconfig' and the filter-commands might be different.

That's not a java-solution and it's not portable - depends on language-settings for instance - but perhaps leads to a pragmatical solution.


http://home.arcor.de/hirnstrom/bewerbung
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Those commands won't work if you are part of an internal network. They will only give you your internal IP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Learn your IP number and email it??