Last active
December 25, 2015 20:29
-
-
Save eralpkaraduman/7035919 to your computer and use it in GitHub Desktop.
AWS route 53 dynamic ip update tool.
This file contains 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 AWS = require('aws-sdk'); | |
AWS.config.loadFromPath('config.json'); | |
var route53 = new AWS.Route53(); | |
var net = require('net'); | |
var os = require('os'); | |
var zone = "YOUR_ZONE_ID"; | |
var domain = "yourdoma.in"; | |
var lastKnownIp = null; | |
var lastSentIp = null; | |
var request = require("request"); | |
function checkIp(){ | |
var repeater = "http://YOUR_IP:YOUR_PORT"; // FOR a simple service that returns the ip of the caller as text.plain | |
request(repeater, function(error, response, body) { | |
if(error || body == "undefined"){ | |
lastKnownIp = null; | |
console.log("can't get current ip from repeater "+repeater); | |
}else{ | |
lastKnownIp = body; | |
if(lastKnownIp != lastSentIp){ | |
sendNewIp(lastKnownIp); | |
}else{ | |
console.log("ip not changed"); | |
} | |
} | |
setTimeout(checkIp,60*1000); | |
}); | |
} | |
function sendNewIp(newIp){ | |
console.log("sending new ip:"+newIp+"..."); | |
getCurrentRecord(function(ip_err,ip_data){ | |
if(ip_err){ | |
console.log("can't get current record: "+ip_err); | |
return; | |
} | |
route53.changeResourceRecordSets({ | |
"HostedZoneId":zone, | |
"ChangeBatch":{ | |
"Changes":[ | |
{"Action":"DELETE", | |
"ResourceRecordSet":{ | |
"Name":domain, | |
"Type":"A", | |
"TTL":300, | |
"ResourceRecords":ip_data | |
} | |
}, | |
{"Action":"CREATE", | |
"ResourceRecordSet":{ | |
"Name":domain, | |
"Type":"A", | |
"TTL":300, | |
"ResourceRecords":[{"Value":newIp+""}]} | |
} | |
] | |
} | |
},function(err,data){ | |
if(!err){ | |
console.log("sending new ip:"+newIp+" OK"); | |
//console.log(data); | |
lastSentIp = newIp; | |
}else{ | |
console.log("sending new ip:"+newIp+" FAILED"); | |
console.log("err "+err); | |
} | |
}) | |
}); | |
} | |
function getCurrentRecord(callback){ | |
route53.listResourceRecordSets({ | |
"HostedZoneId":zone | |
},function(err,data){ | |
if(err){ | |
callback(err,null); | |
}else{ | |
var existingIps = null; | |
for (var i=0; data.ResourceRecordSets.length; i++){ | |
var recordSet = data.ResourceRecordSets[i]; | |
if(recordSet.Type == "A"){ | |
existingIps = recordSet.ResourceRecords; | |
break; | |
} | |
} | |
callback(null,existingIps); | |
} | |
}); | |
} | |
checkIp(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment