-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-dns.php
139 lines (111 loc) · 3.42 KB
/
dynamic-dns.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Dynamic DNS Updater
*
* Very simple php script to do dynamic DNS updates to linode's free DNS service.
* The script uses no third party externsions or plugins. All that is required is
* php5-cli & php5-curl. Please make sure the initial DNS entry has been manually
* created at the linode end.
*
* LICENSE: GNU GENERAL PUBLIC LICENSE http://www.gnu.org/licenses/gpl.html
*
* @credit: written by alex.bergin@gmail.com, but based partially on
* http://blog.pathennessy.org/2009/05/11/linode-dynamic-dns-bash-script/
*/
// Set your variables here
$api_url = "https://api.linode.com/api/";
$api_key = "./.linode-apikey";
$last_ip_file = "/tmp/lastip";
$domain = "example.com";
$subdomain = "homeip";
$ip_source = "http://api.externalip.net/ip/"; // free service to determine external IP address.
// End of variables
// Get the api_key
$api_key_value = file_get_contents($api_key);
$default_curl_arguments = array(
CURLOPT_URL => $api_url,
CURLOPT_POST => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE, // turn off SSL check, as php might not have the allowed certs
CURLOPT_RETURNTRANSFER => true, // tell curl to return the contents rather than to STDOUT
);
// Get your current IP and determine if we should proceed.
$ip = file_get_contents($ip_source);
$last_ip = false;
if (file_exists($last_ip_file)) {
$last_ip = file_get_contents($last_ip_file);
}
if ($ip == $last_ip) {
exit; // don't update as nothing has changed.
}
/**
* Get the linode DOMAIN ID dynamically.
*/
$fields = array(
'api_key' => $api_key_value,
'action' => 'domain.list',
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt_array($ch, $default_curl_arguments);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$json_result = curl_exec($ch);
curl_close($ch);
$json = json_decode($json_result);
if (!is_array($json->DATA)) {
echo "Error: no json data returned from linode during domain list request.";
exit;
}
$DOMAINID = false;
while (!$DOMAINID && list(,$item) = each($json->DATA)) {
if ($item->DOMAIN == $domain) {
$DOMAINID = $item->DOMAINID;
}
}
/**
* Get the linode RESOURCE ID dynamically.
*/
$fields = array(
'api_key' => $api_key_value,
'action' => 'domain.resource.list',
'DomainID' => $DOMAINID
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt_array($ch, $default_curl_arguments);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$json_result = curl_exec($ch);
curl_close($ch);
$json = json_decode($json_result);
if (!is_array($json->DATA)) {
echo "Error: no json data returned from linode during resourse list request.";
exit;
}
$RESOURCEID = false;
while (!$RESOURCEID && list(,$item) = each($json->DATA)) {
if ($item->NAME == $subdomain) {
$RESOURCEID = $item->RESOURCEID;
}
}
/**
* Set the dynamic IP
*/
$fields = array(
'api_key' => $api_key_value,
'action' => 'domain.resource.update',
'DomainID' => $DOMAINID,
'ResourceID' => $RESOURCEID,
'Target' => $ip,
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt_array($ch, $default_curl_arguments);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$json_result = curl_exec($ch);
curl_close($ch);
$json = json_decode($json_result);
if (!isset($json->DATA->ResourceID)) {
echo "Error: no json data returned from linode during domain.resource.update.";
exit;
}
// If the update was successful, store the ip in the last ip file.
file_put_contents($last_ip_file, $ip);