From 9d732aad41b3de8bd1c607d0df974d8083b5899c Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Tue, 16 Aug 2016 00:44:10 +0200 Subject: [PATCH] First, working version... Usage: php ./update.php host example.com --- .gitignore | 1 + README.md | 0 config.php.sample | 6 +++++ update.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 .gitignore delete mode 100644 README.md create mode 100644 config.php.sample create mode 100644 update.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f4773f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.php diff --git a/README.md b/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/config.php.sample b/config.php.sample new file mode 100644 index 0000000..afdfda9 --- /dev/null +++ b/config.php.sample @@ -0,0 +1,6 @@ +<?php +$soap_location = 'https://ispconfig.example.com:8080/remote/index.php'; +$soap_uri = 'https://ispconfig.example.com:8080/remote/'; +$soap_user = 'username'; +$soap_password = 'password'; + diff --git a/update.php b/update.php new file mode 100644 index 0000000..b113ec1 --- /dev/null +++ b/update.php @@ -0,0 +1,66 @@ +<?php +require('config.php'); + +if ($argc<3) { + print "Usage: php ./update.php HOST DOMAIN [IP]\n"; + print "If not given, the ip address will be queried from icanhazip.com\n"; + die(); +} + +$ddns_host = $argv[1]; +$domain = $argv[2]; +if ($argc>3) { + $ip = $argv[3]; +} else { + // Figure out the public IP of this host: + $ip = trim(file_get_contents("http://icanhazip.com/")); + if (filter_var($ip, FILTER_VALIDATE_IP) === false) { + die("Unable to retrieve public IP address (icanhazip.com returned $ip)\n"); + } +} + +// print("Setting DDNS host $ddns_host.$domain to IP $ip\n"); + +$client = new SoapClient(null, + array('location' => $soap_location, + 'uri' => $soap_uri, + 'trace' => 1, + 'exceptions' => 1)); + +try { + $session_id = $client->login($soap_user, $soap_password); + + $zone_id = $client->dns_zone_get_id($session_id, $domain); + $zone = $client->dns_zone_get($session_id, $zone_id); + $records = $client->dns_rr_get_all_by_zone($session_id, $zone_id); + + $dns_record = null; + foreach ($records as $rec) { + if ($rec['type']=='A' && $rec['name']==$ddns_host) { + $dns_record = $rec; + } + } + if (is_null($dns_record)) { + $client->logout($session_id); + die("Unable to find DNS record for host $ddns_host in domain $domain on the server...\n"); + } + if ($dns_record['data'] != $ip) { + $dns_record['data'] = $ip; + $dns_record['serial'] = $dns_record['serial']+1; + $client->dns_a_update($session_id, null, $dns_record['id'], $dns_record); + + $zone['serial'] = $zone['serial'] + 1; + $client->dns_zone_update($session_id, 0, $zone_id, $zone); + + print("Successfully set DNS entry for host $ddns_host in domain $domain to $ip.\n"); + } else { +// print("IP address of $ddns_host.$domain already set to $ip.\n"); + } + + $client->logout($session_id); + +} catch (SoapFault $e) { + die('SOAP Error: '.$e->getMessage()."\n"); +} + +?> \ No newline at end of file -- GitLab