Skip to content

Instantly share code, notes, and snippets.

@loriopatrick
Created March 20, 2014 20:08
Show Gist options
  • Save loriopatrick/9672731 to your computer and use it in GitHub Desktop.
Save loriopatrick/9672731 to your computer and use it in GitHub Desktop.
Send an email to an admin when the public ip changes.
var exec = require('child_process').exec;
function getIp(callback) {
exec('wget -qO- http://ipecho.net/plain ; echo', function (error, stdout, stderror) {
if (error) {
callback(error, null);
return;
}
var ip = stdout.replace('\n', '');
callback(null, ip);
});
}
function sendEmail(subject, body, email, callback) {
var command = [
'echo "', body.replace("\\", "\\\\").replace('"', '\\"'),
'" | mail -s "', subject.replace("\\", "\\\\").replace('"', '\\"'), '" ',
email
].join('');
exec(command, function (error, stdout, stderror) {
if (callback) callback(error);
});
}
function updateAdminOnChange(adminEmail, timeout) {
var ipAddress = '';
function update() {
getIp(function (error, ip) {
if (error) {
sendEmail('Error', error, adminEmail);
return;
}
if (ipAddress != ip) {
var message = [
'Old IP: ', ipAddress,
'New Ip: ', ip
].join('\n');
sendEmail('Ip Update', message, adminEmail);
ipAddress = ip;
console.log('new ip', ip);
}
});
}
setInterval(update, timeout);
}
updateAdminOnChange('[email protected]', 86400000/4); // update 4x a day
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment