Skip to content

Instantly share code, notes, and snippets.

@dogmatic69
Last active August 29, 2015 14:00
Show Gist options
  • Save dogmatic69/11358197 to your computer and use it in GitHub Desktop.
Save dogmatic69/11358197 to your computer and use it in GitHub Desktop.
DynaDNS type script for Rackspace based DNS

Poormans DynaDNS with rackspace

Uses online site to check current ip address which is saved to a file, when re-run the saved ip is compared with a check of the current again and if it is different will log into the rackspace API to remove the old entry and create a new one.

Best to have the DNS entry with a very short TTL, rackspace has 300 seconds as the shortest.

The mail file can be replaced with anything, or just blank for no action. By default it will mail if there has been a change or a problem processing the new ip address.

Add a cron:

0 * * * * /path/to/dns-update.sh 

Run manually:

/path/to/dns-update.sh -v 
#!/usr/bin/php
<?php
$cacheFile = './ipaddress';
$rackspace = array(
'user' => 'rackspace_username',
'api_key' => 'rackspace_api_key',
'domain_id' => 123456, // domain where the subdomain is located
'dns_name' => 'something.foo.com', // record being updated
);
$options = array_merge(array('v' => null, 'h' => null), getopt('vh'));
function pr($v) {
echo sprintf('<pre>%s</pre>', print_r($v, true));
}
$oldIp = trim(`cat $cacheFile`);
$command = "curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'";
$newIp = trim(`$command`);
if ($options['v'] !== null) {
echo "Old IP: $oldIp\n";
echo "New IP: $newIp\n\n";
}
if ($newIp == $oldIp) {
if ($options['v'] !== null) {
echo "No change\n";
}
exit(0);
}
require_once "rackspace.php";
$dns = new rackDNS($rackspace['user'], $rackspace['api_key']);
$results = $dns->list_domain_details($rackspace['domain_id'], true, true);
foreach ($results['recordsList']['records'] as $record) {
if ($record['name'] == $rackspace['dns_name']) {
if ($options['v'] !== null) {
echo "DNS record deleted\n";
}
$dns->delete_domain_record($rackspace['domain_id'], $record['id']);
break;
}
}
$result = $dns->create_domain_record($rackspace['domain_id'], $dns->create_domain_record_helper('A', $rackspace['dns_name'], $newIp, 300));
if (empty($result) || $result['status'] != 'COMPLETED') {
$subject = 'Error: Failed to update IP address';
$body = sprintf("Failed to update IP address\n\n%s\n", print_r($result, true));
include 'mail.php';
exit(1);
}
if ($options['v'] !== null) {
echo "DNS record updated\n\n";
}
$command = "echo \"$newIp\" > $cacheFile";
`$command`;
$subject = 'IP address changed';
$body = sprintf('New IP: %s', $newIp);
include 'mail.php';
exit(0);
<?php
/**
* Uses PEAR mail for SMTP
*/
require_once "Mail.php";
$to = 'email@domain.com';
$from = 'email@domain.com';
$headers = array(
'From' => '<' . $from .'>',
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => $from,
'password' => 'password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
return false;
}
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment