This repository was archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp-ddns-client.php
More file actions
117 lines (84 loc) · 2.75 KB
/
php-ddns-client.php
File metadata and controls
117 lines (84 loc) · 2.75 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
* This is a simple client for updating hosts at a Dynamic DNS service
* provider.
*/
/*
* Copyright (C) 2011 Michael Bemmerl
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Start of configuration area */
$user = 'test'; // Your DDNS username
$pass = 'test'; // Your DDNS password
$hosts[] = 'test.mine.nu'; // The hosts you want to update
//$hosts[] = 'another-host.dyndns.org';
$force = FALSE; // Set to TRUE if you want to force an update
/* End of configuration area */
function ddns_get_ipaddress()
{
$content = @file_get_contents('http://checkip.dyndns.com');
$matched = $content !== FALSE && (bool)preg_match('/\d*\.\d*\.\d*\.\d*/', $content, $matches);
if ($matched)
return $matches[0];
else
return FALSE;
}
function ddns_resolve_host($host)
{
$ips = dns_get_record($host, DNS_A);
if ($ips === FALSE)
return FALSE;
$result = array();
foreach($ips as $ip)
$result[] = $ip['ip'];
return $result;
}
function ddns_update_host($host, $newIP)
{
global $user, $pass;
$url = sprintf('http://%s:%s@members.dyndns.org/nic/update?hostname=%s&myip=%s', $user, $pass, $host, $newIP);
$opts = array('http' => array('user_agent' => 'php-ddns-client 0.1'));
$context = stream_context_create($opts);
$content = file_get_contents($url, false, $context);
$matched = $content !== FALSE && (bool)preg_match('/^\w*/', $content, $matches);
if ($matched === FALSE)
return FALSE;
$result = strtolower($matches[0]);
return $result == 'good';
}
function ddns_error($message, $die = TRUE)
{
header('HTTP/1.0 500 Internal Server Error');
print $message;
if ($die)
die();
}
header('Content-Type: text/plain');
$currentIP = ddns_get_ipaddress();
if ($currentIP === FALSE)
ddns_error('Could not detect external IP address.');
foreach($hosts as $host)
{
print 'Updating \'' . $host . '\': ';
if (!$force)
$oldIPs = ddns_resolve_host($host);
if ($force || !in_array($currentIP, $oldIPs, TRUE))
{
$result = ddns_update_host($host, $currentIP);
print $result ? 'success' : 'failed!';
}
else
print 'not neccessary';
print PHP_EOL;
}