PHP EditDNS Updater for Linux

This is a simple PHP script which will update your dynamic DNS records using EditDNS.

Features:
– You can configure as many records as you want.
– Supports HTTP and HTTPS.
– For PHP 4x and higher compiled with sockets.
– Simple validation.

Here is the code: (editdns.php)

#!/usr/bin/php -q
<?php

$port = 443; // use 80 if you dont wanna use SSL
// add as many arrays as you want, first element is the record
// and the second is the password
// $data[] = array('myrecord1.domain.com','mypass1');
// $data[] = array('myrecord2.domain.com','mypass2');
// ... etc etc etc ;)
$data[] = array('somerecord.resolveme.com','blahblah123');
$sleep = 1; // seconds we should sleep before updating another record

// main loop
if (!is_array($data)) die("Nothing to do\n");
foreach ($data as $v) {
  echo "Updating $v[0] ... ";
  if(_send($port,"p=$v[1]&r=$v[0]")) echo "[OK]\n";
  sleep($sleep);
}

function _send($port=443,$args="") {
  if ($port == 443) $proto = "ssl";
  else $proto = "http";
  $fp = fsockopen("$proto://dyndns.editdns.net",$port,$errno,$errstr,10);
  if (!$fp) die("\nCouldn't establish connection\n");
  $out = "POST /api/dynLinux.php HTTP/1.0\r\n";
  $out .= "Host: dyndns.editdns.net\r\n";
  $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out .= "Content-Length: ".strlen($args)."\r\n";
  $out .= "Connection: Close\r\n\r\n";
  $out .= $args;
  //echo $out;
  fwrite($fp, $out);
  while (!feof($fp)) {
    $res .= fgets($fp,1024);
  }
  fclose($fp);
  $parts = explode("\r\n\r\n",trim($res));
  //print_r($parts);
  // checking output
  if (preg_match("/record has been updated|record already exists/i",$parts[1])) return 1;
  // anything else should be an error
  echo "[ERROR]\n\t$parts[1]\n";
  return 0;
}

?>

Save it, chmod +x and play with it.

Old sources:
http://forums.nerdie.net/showthread.php?t=616
http://xux.in/blog/post/ip-updater-for-editdnsnet/

IP updater for EditDNS.net

As anyone know (and should know) EditDNS it’s the best alternative for DNS Management and the best of all it is FREE 😉

Here I wrote/adapted some code which will allow you to update your dynamic IP through EditDNS’s API.

Requirements:

  • You need to register first! (duh)
  • Donations are optional, but if it makes your life easier you should consider it and you’ll also get more services.
  • Perl!

File: editdns.pl

#!/usr/bin/perl

use strict;

## Configure ONLY this 2 variables
my $editdns_pass   = "a"; # put your password
my $editdns_record = "b"; # put the record you wish to update

## ###############
## Nothing else should be changed unless you know what to do
## ###############

my $host = "DynDNS.EditDNS.net";
my $port = 80;
my $editdns_post = "p=$editdns_pass&r=$editdns_record";

my $editdns_req = join("",
  "POST /api/dynLinux.php HTTP/1.0\r\n",
  "Host: $host:$port\r\n",
  "User-Agent: EditDNS Browser 0.1\r\n",
  "Referer: http://www.editdns.net\r\n",
  "Content-Type: application/x-www-form-urlencoded\r\n",
  "Content-Length: ".length($editdns_post)."\r\n\r\n",
  "$editdns_post\n"
);

my $hostaddr = (gethostbyname($host))[4] || &error("Couldn't get IP for $host");
my $remotehost= pack('S n a4 x8',2,$port,$hostaddr);
socket(S,2,1,6) || &error("Couldn't create socket");
connect(S,$remotehost) || &error("Couldn't connect to $host:$port");
select((select(S),$|=1)[0]);
print S $editdns_req;
vec(my $rin='',fileno(S),1)= 1 ;
select($rin,undef,undef,60) || &error("No response from $host:$port");
undef($/);
close(S);
print "[DONE]\n";
exit;

sub error {
        print "[ERROR] $_[0]\n";
        exit;
}

Next and once you have configured the script:

chmod +x editdns.pl
pico /etc/crontab
# Add editdns.pl to execute every 15 minutes
*/15 * * * * root /path/editdns.pl > /dev/null 2>&1

Do not set intervals lower than 15 minutes, since it can be considered as an abuse and you’ll get banned.

Part of this code was taken from James Marshal, happy coding!

*** If you are looking for SSL support and multiple records you might want to check http://xux.in/blog/post/php-editdns-updater-for-linux/