Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/6666a04295fb99b9b488 to your computer and use it in GitHub Desktop.
Save lgaetz/6666a04295fb99b9b488 to your computer and use it in GitHub Desktop.
<?php
/**** **** **** **** **** **** **** **** **** **** ****
* Description:
* Script that checks the ip address of a specified
* host and sends an email alert if the host's
* ip address has changed since the last check
*
* Usage:
* Run as a cron job to monitor an IP address, generates
* output so pipe to null or set $debug to false
*
* Version history:
*
* 2015-05-25 First commit
* 2015-05-28 added check to catch host lookup failure
*
*
* License:
* GNU GPL2+
**** **** **** **** **** **** **** **** **** **** ****/
if ($_GET['host'] && $_GET['email']) {
$host = $_GET['host'];
$email = $_GET['email'];
} else {
// hard code if not passing to script via http
$host = "www.example.com";
$email = "email@example.com";
}
// set to false to suppress output
$debug = true;
// resolve host
$current_ip_address = gethostbyname($host);
// gethostbyname will return the host string if the lookup fails, so check string for valid ip format
if (inet_pton($current_ip_address )) {
$old_ip_address = file_get_contents($host.".txt");
if ($debug) echo "</p>Host: ".$host."</p>Current IP address: ".$current_ip_address."</p>Old IP address: ".$old_ip_address;
if ($current_ip_address != $old_ip_address){
// ip has changed, update file and send notice
$foo = file_put_contents ($host.".txt",$current_ip_address);
if ($debug) echo "</p>New IP address - sending email alert to: ".$email;
$foo = mail ($email,"$host Dynamic IP Has Changed","New Dynamic IP for:\r\n Host: $host\r\n New IP: $current_ip_address");
} else {
if ($debug) echo "</p>IP address unchanged, no alert";
}
} else {
if ($debug) echo "Host lookup failure";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment