Skip to content

Instantly share code, notes, and snippets.

@abirkill
Created August 25, 2018 03:45
Show Gist options
  • Save abirkill/3fa8338c48caaa0dd79ecdcb4d8aab52 to your computer and use it in GitHub Desktop.
Save abirkill/3fa8338c48caaa0dd79ecdcb4d8aab52 to your computer and use it in GitHub Desktop.
Update an existing Linode DNS A or AAAA record to a supplied IP address using the Linode v4 API. Can be used with the pfSense custom dynamic DNS client.
<?php
/* An extremely basic example using PHP to update a Linode DNS entry via the
Linode v4 API (<https://developers.linode.com/api/v4>).
Takes an IP address as a GET parameter.
Can be used with pfSense's custom Dynamic DNS service (add a new Dynamic
DNS client, choose service type of 'Custom' or 'Custom (v6)' for IPv6, add
an update URL of 'https://yourlinode.com/yourscript.php?ip=%IP%', and add
a result match of 'OK'.
This script is not production-ready! You will at least want to add:
- Authentication -- prevent anyone who finds your update URL from
updating your DNS record without your permission!
- Validation -- ensure that a valid IP address has been passed into the
script.
- Retry logic -- handle situations where the Linode API is down.
You will require:
- An API personal access token with Domain Read/Write permissions:
<https://www.linode.com/docs/platform/api/getting-started-with-the-linode-api/>
- Your domain ID, accessible by viewing the Zone File from the Linode DNS
manager. The domain ID is the number in square brackets on the first
line, just after the domain name.
- The record ID, accessible by editing the A/AAAA record in the DNS
manager. The record ID is the number after the id field in the URL.
Note: This script will not create a record, you need an existing
record for this script to update.
*/
$token = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
$domain = "123456";
$record = "123456";
if(!isset($_GET['ip'])) {
echo "FAIL (no IP supplied)";
exit(1);
}
$ip = $_GET['ip'];
$url = "https://api.linode.com/v4/domains/" . $domain . "/records/" . $record;
$data = array("target" => $ip);
$json = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token,
'Content-type: application/json',
'Content-length: ' . strlen($json)
));
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
echo "OK";
exit(0);
} else {
echo "FAIL (API returned error)\n";
echo "Return code: " . curl_getinfo($curl, CURLINFO_HTTP_CODE) . "\n";
print_r($response);
exit(1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment