Skip to content

Instantly share code, notes, and snippets.

@lewayotte
Last active March 17, 2022 09:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lewayotte/35424c0d34014e1ed1fa to your computer and use it in GitHub Desktop.
Save lewayotte/35424c0d34014e1ed1fa to your computer and use it in GitHub Desktop.
Cloud Flare Automatic DNS Record Updater (PHP Script)
#!/usr/bin/php
<?php
/**
* Script used to update DNS records in Cloud Flare when server IP address changes.
* My use case is to run this script on startup, when I spin up a new Digital Ocean VPS but
* this could easily be used to update for dynamic DNS with a cronjob.
*
* Digital Ocean referral code: https://www.digitalocean.com/?refcode=3655e259ce29
*
* Requires PHP cURL
*
* Copy to /usr/local/bin/
* chmod +x /usr/local/bin/cloudflare-dns.php
*
* OR curl https://gist.githubusercontent.com/lewayotte/35424c0d34014e1ed1fa/raw/91740c4d678162ba95dcc8e6c9ed4aac75de54b0/cloudflare-dns.php > /usr/local/bin/cloudflare-dns.php && chmod +x /usr/local/bin/cloudflare-dns.php && vi /usr/local/bin/cloudflare-dns.php
*
* In Ubuntu, I added a simple sh script to /etc/network/if-up.d/cloudflare to the network has been setup.
* #!/bin/sh
* /usr/local/bin/cloudflare-dns.php > /dev/null
* exit 0
*
* But to do this as a cronjob, you could simply run `crontab -e` and something like this:
* * * * * * /usr/local/bin/cloudflare-dns.php > /dev/null
*
*/
$token = 'API KEY from Cloud Flare'; //https://www.cloudflare.com/my-account
$email = 'Cloud Flare Account Email';
$domain = 'domain.tld';
$hostname = php_uname( 'n' ); //Gets current hostname
$new_ip = trim( `/sbin/ip addr show eth0 | grep "inet " | awk '{print $2}' | cut -f1 -d'/'` ); //Gets server's IP Address
$old_ip = trim( `host $hostname | grep 'has address' | awk '{print $4}'` ); //Gets DNS's IP Address
if ( $new_ip !== $old_ip ) {// iIf the IP addresses are the same, we don't need to make any changes
$api_url = 'https://www.cloudflare.com/api_json.html';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$body = array(
'a' => 'rec_load_all',
'tkn' => $token,
'email' => $email,
'z' => $domain,
);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
if ( $output = curl_exec( $ch ) ) {
$results = json_decode( $output );
if ( 'success' === $results->result ) {
foreach( $results->response->recs->objs as $dns_entry ) {
if ( $dns_entry->name === $hostname ) {
$record_id = $dns_entry->rec_id;
$new_entry = false;
break;
} else {
$new_entry = true;
}
}
if ( $new_entry ) {
$body = array(
'a' => 'rec_new',
'tkn' => $token,
'email' => $email,
'z' => $domain,
'type' => 'A',
'name' => $hostname,
'content' => $new_ip,
'ttl' => '1',
);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
if ( $output = curl_exec( $ch ) ) {
$results = json_decode( $output );
if ( 'success' === $results->result ) {
echo "IP Address added for hostname.\n";
} else {
printf( "Error adding ip address for hostname: %s\n", $results->msg );
}
} else {
echo "Error adding IP Address for hostname.\n";
}
} else {
$body = array(
'a' => 'rec_edit',
'tkn' => $token,
'email' => $email,
'z' => $domain,
'id' => $record_id,
'type' => 'A',
'name' => $hostname,
'content' => $new_ip,
'ttl' => '1',
);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
if ( $output = curl_exec( $ch ) ) {
$results = json_decode( $output );
if ( 'success' === $results->result ) {
echo "IP Address updated for hostname.\n";
} else {
printf( "Error adding ip address for hostname: %s\n", $results->msg );
}
} else {
echo "Error changing IP Address for hostname.\n";
}
}
} else {
printf( "Error adding ip address for hostname: %s\n", $results->msg );
}
}
curl_close( $ch );
} else {
echo "No changes required\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment