-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathddns53.php
More file actions
62 lines (48 loc) · 1.82 KB
/
Copy pathddns53.php
File metadata and controls
62 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
require_once('r53.php');
require_once('IncR53.php');
$awszoneid='/hostedzone/'.$awszoneid;
$r53 = new Route53($awsid, $awskey);
function getPublicIP() {
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, "http://icanhazip.com");
// grab URL and pass it to the browser
$ip = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$ip = trim(preg_replace('/\s+/', ' ', $ip));
return $ip;
}
function updateIP ($hostname, $newIP, $oldIP, $awszoneid, $type = 'A', $ttl = 60 ) {
global $r53;
$delete = $r53->prepareChange('DELETE', $hostname, $type, $ttl, $oldIP);
$result = $r53->changeResourceRecordSets($awszoneid, $delete);
$create = $r53->prepareChange('CREATE', $hostname, $type, $ttl, $newIP);
$result = $r53->changeResourceRecordSets($awszoneid, $create);
}
//print_r($r53->listHostedZones());
//print_r($r53->getHostedZone($awszoneid));
$recordSet = $r53->listResourceRecordSets($awszoneid);
//print_r($recordSet['ResourceRecordSets']);
for ($i = 0; $i < count($recordSet['ResourceRecordSets']); $i++) {
if ($recordSet['ResourceRecordSets'][$i]['Name'] == $hostname) {
// print_r($recordSet['ResourceRecordSets'][$i]);
// echo $recordSet['ResourceRecordSets'][$i]['Name'];
$oldIP = $recordSet['ResourceRecordSets'][$i]['ResourceRecords'][0];
$type = $recordSet['ResourceRecordSets'][$i]['Type'];
$ttl = $recordSet['ResourceRecordSets'][$i]['TTL'];
// echo $oldIP;
}}
$newIP = getPublicIP();
if ($oldIP == $newIP) {
// echo "No change necessary.\n";
} else {
echo "Updating ".$hostname." from ".$oldIP." to ".$newIP."\n";
updateIP($hostname, $newIP, $oldIP, $awszoneid);
// echo " done.";
}
?>