ericfaller.com Pithy Quote Goes Here

9Oct/07

Detecting IP address changes

My brother recently asked me about writing a program that would detect when his IP address changes and email it to him. I thought "Oh, that should be easy to write".

I had it about half-way written out, when I realized a problem: it's easy to detect when the local computer's IP address changes, but with home routers these days, the IP address is going to be something like 192.168.0.2, and never change. What you really want is to detect when your router's IP address changes. That's harder than it sounds, though - the whole purpose of NAT is to effectively mask the fact that you aren't actually directly connected to the real internet.

I thought about it for a while but couldn't think of a solution that didn't involve contacting a remote server and having it tell you your IP address. It looks like dynamic DNS providers like DynDNS also have the same problem but no better solutions. It might be possible to connect to the router itself and get the IP from its status page, but that would be pretty router-specific.

In the end, this is the one-line C# program I came up with which will email you your internet-facing IP address:

new SmtpClient("smtp_server_name").Send(new MailMessage( "from@address.com", "to@address.com", "New IP (subject)", "Your new IP address is: " + new Regex( @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" ).Match(new WebClient( ).DownloadString( "http://checkip.dyndns.org/")).Value));

Just compile that and set it as a scheduled task to run every 24 hours and you should be good to go :) . Of course, if you only want it to mail you when the IP address actually changes, or if your SMTP server requires authentication, then it might require a couple more lines of code...

Comments (7) Trackbacks (0)
  1. Here is a question that will have you thinking I’m a complete idiot, but I’m really curious so I’ll risk it – why is it important to know when your IP address changes?

    Also, not the same topic, but related, why do some people want to hide their IP addresses? I’ve heard of this and wondered…

  2. No that’s a perfectly valid question :) . Your IP address is basically a unique number that identifies your computer on the internet. It’s used when making connections in and out of your computer. If you know what your IP address is, and your computer is sitting at home turned on, then you can connect to it and do things like log in, access your files, listen to your music, etc. The problem is that your ISP will sometimes change your IP address on you since they only have so many of them, and they ration them out to their customers based on who’s connected at any given time. Thus the need to know when your IP changes – the old IP might now be pointing to somebody else’s computer, and you need to get the new one in order to connect to your computer again.
    As for hiding your IP, you might want to do that for various reasons. When you connect to a web site, your computer sends its IP address to the web site so that it knows where to send the data back to. That computer will probably log the IP address in order to calculate some statistics. If somebody has access to a wide set of logs, they can correlate your actions together, based on your IP. For example, let’s say you have a GMail account and you read some email, and then you go do a search for “how to axe-murder somebody”, then Google knows that it was probably you searching for that, since it came from the same IP address as your GMail session. This sort of stuff can be used as evidence in court, or when the record companies sue you for downloading an MP3, etc. Other things such as ‘cookies’ make it even easier to track you, even when your IP changes.
    You might also want to hide your IP address if you’re posting anonymously on a web site and don’t want people to figure out who you are. If the web site shows the IP addresses of people who post, then other people can come by and look up that IP to try to figure out who you are. Since the IP addresses are owned by the ISP instead of the customers, it’s usually possible to track back to which ISP you have, and which city you live in. If you want to get somebody’s name, you either have to subpoena the ISP or correlate the IP with other things, like described above.
    Wow that ended up being pretty long, it’s almost a full post :)

  3. My IP address with Comcast stayed the same for two years, but after ongoing trouble with my router, I downgraded the firmware and it changed! I host websites from home, and for some silly reason, even though port 80 was still forwarded to my server, I could view sites fine from work (Time Warner cable) but not anywhere else. I have no idea what was going on. I had to reset the router settings and that worked.

    Perhaps you can use uPNP to query the IP address from the router.

  4. I sent a comment but then my computer went wonky, so I don’t know if it will show up here or not.
    Let’s see…I thanked you for the great explanations. Then I said that I only used my computer at work and at home for basic functions, so I didn’t realize the need to know if IP addresses had changed. And then I think I asked if bigger ISPs had more IP addresses to provide to users, so they would change less frequently…or would it be that smaller ISPs with fewer customers would change less frequently? Also, why aren’t there an unlimited number of IPs?

  5. Hmm I don’t see the other comment either. It must have gotten eaten :( .
    It probably depends on each individual ISP, more than how big they are. They can buy them in blocks, so they all probably just buy up as many as they should need. That said, bigger ISPs have a lot more money, so they can easily afford to get more IPs if there’s a crunch.
    Why there aren’t unlimitd IPs is a good question :) . Back in the 80s when the military and universities were designing the internet, they didn’t imagine that one day everybody in the world might be connected, and they only made the IP numbers long enough for about 4 billion of them – there just aren’t enough digits in the numbers to have more. This is obviously becoming a big problem now that there are more than 4 billion people in the world, so they’re trying to move to a new IP system known as “IPv6″, where the numbers are a lot longer and have enough space for zillions more IPs, effectively making them unlimited. But that’s taking a long time to happen, since they effectively have to upgrade the whole internet infrastructure, and all the computers on it..

  6. Good answers! Are you getting tired of answering my questions? I swear, I am not a four year old in the “Why?” stage. I just think this is interesting!

    Military? Universities? So, are you saying that Al Gore did not invent the Internet? :)

    ISPs are buying the IP addresses in blocks from ? Who?

    You’re doing way too good of a job of answering me because I am following what you’re saying and it’s making me think about this more. You should be teaching!

  7. lol, thanks :) Given that it’s the middle of the day, I should probably be working, instead of teaching.. hmm.

    And yeah Al Gore did not really invent the internet :) . I think his claim was that he submitted the original bill in the senate in the 70′s that funded the military to create the internet.

    I am not really familiar with the process for getting IP blocks, never having done it myself, but basically the Internet Assigned Numbers Authority gives them out. It was formerly a company run by the US goverment, but now it’s a private company, and there is a big debate in world politics about whether it should be taken over by the UN or some other international standards body.

Leave a comment


No trackbacks yet.

About Me

Eric Faller

Eric Faller

Software Engineer at Facebook
Palo Alto, CA
Resumé
Facebook Link

Like

Photos

DSC01629 IMG_2154 IMG_4908 DSC02002 DSC01092 DSC01215 DSC_5337 DSC_4330 IMG_2423 DSC_5104 DSC_4463 IMG_0594
More Photos..

Recent Posts

Topics

Archives

Site Tasks