Created
March 20, 2014 20:08
-
-
Save loriopatrick/9672731 to your computer and use it in GitHub Desktop.
Send an email to an admin when the public ip changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('"', '\\"'), '" ', | |
].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