Notify myself upon IP Changes via email for Home Servers
By rockia on May 19, 2011 with Comments 0
I have set up a server at home and I do most of the PHP testing upon that server. Other than HTTP server, it also serves as a FTP server and a remote desktop (I am a Mac user, and I can connect back home whenever I need Windows). As I noted before, most the ISP will block the port 80 and 21, so I use no-ip as my dynamic IP provider that will turn my difficult-to-memorize IP address into something I can recognize and remember. So all I need to memorize is something like http://MYDOMAIN.no-ip.org/ and it will forward my request to my assigned IP address on which my home server is located.
I am using Telus as my ISP and sometimes my IP address will change when there is a signal cut-off (Trust me, it happens all the time.). The admin panel downloaded from no-ip.com is supposed to help me update the IP address record whenever it’s changed, yet I don’t know why it doesn’t really work for me. I actually found it out recently when I couldn’t resolve my no-ip DNS when my home server was running perfectly. I know I should take a look at no-ip’s FAQ or send them an email to resolve the problems, but as a programmer (I think I am), I would like to use some scripting to help me out.
So my idea is as following:
- Keep a copy of my IP address in the same folder as my PHP script (I will make it in a text file for ease, but there is no problem to store it in database and keep track of the changes);
- Write a page of PHP script to check the current server’s public IP address. If it matches the record found in the text file, then we call it done. If not, then we first update the text file, and then email me about the changes, so that I can go to no-ip’s website and change it manually.
- Use Crontab to create a new schedule and visit the PHP page, in my case, I make it run every 30 minutes.
Here is what I have done:
PHP Script:
<?php
$content = file('ip.txt');
$ip=file_get_contents('http://www.rockia.com/ip.php');
if($content[0] != $ip){
//When IP is changed
$handle = fopen('ip.txt','w');
fwrite($handle,$ip);
fclose($handle);
$headers = 'From: myemail@fromemail.com' . "\r\n" . //Replace the email with your FROM email
'Reply-To: reply-to@fromemail.com' . "\r\n" . //Replace the email with your REPLY-TO email
'X-Mailer: PHP/' . phpversion();
$to = "target@myemail.com"; //Replace the email with your TO email
$subject = "Public IP changed!";
$body = "The new IP addresss is ".$ip;
mail($to, $subject, $body,$headers);
}else{
//IP address is not changed, do nothing.
}
?>
Server CRON setting
*/30 * * * * wget -q –spider http://127.0.0.1/ip/ip.php
Just a note that if you just use wget ,you will end up having multiple copies of ip.php in your folder, so make sure you include “-q –spider” in the line. What the command line is saying is for every 30 minutes, visit ip.php at local server 127.0.0.1.
So when the PHP script is activated, it will does what it suppose to do. It will scan the folder and look for “ip.txt”; it will also create the file if the file is note found. Then check the file content, if it matches with the current IP address, then call it done, or it will send myself a copy of the IP address.
Filed Under: Programming