PHP IP Update Script


#1

Hi,

I wrote a script to auto-update your IP in the DNS Zone if it has been changed. It can be used on Linux (through systemd or cron), Windows (through Task Scheduler) and Mac too probably but I don’t have one so I can’t test it, or tell you how to do it.

You can use them for multiple domains, just copy it and fill in the needed fields and run them.

The only thing you need is PHP with curl enabled in php.ini.

domain.tld-updater.php (just call them after your domain so it’s easy to remember which one is which)

<?php
$headers = [ 
    'X-Auth-Email: YOUR_EMAIL_ADDRESS',
    'X-Auth-Key: YOUR_CLOUDFLARE_AUTH_KEY',
    'Content-Type: application/json'
];

$domain = "example.com";
$record = "sub.example.com";
$ip = file_get_contents('https://api.ipify.org');

$data = [
    'type' => 'A',
    'name' => $record,
    'content' => $ip,
    'ttl' => '1',
    'proxied' => true
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones?name=$domain");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    exit('Error: ' . curl_error($ch));
}
curl_close ($ch);

$json = json_decode($result, true);

$ZoneID = $json['result']['0']['id'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records?name=$record");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    exit('Error: ' . curl_error($ch));
}
curl_close ($ch);

$json = json_decode($result, true);

$DNSID = $json['result']['0']['id'];

$old_ip = $json['result']['0']['content'];

if ($old_ip === $ip) {
    echo "CloudFlare IP: $old_ip" . PHP_EOL;
    echo "Current IP: $ip" . PHP_EOL;
    echo "The IP doesn't have to be changed!" . PHP_EOL;
}
else {
    echo "CloudFlare IP: $old_ip" . PHP_EOL;
    echo "Current IP: $ip" . PHP_EOL;
    echo "The IP has to be changed!" . PHP_EOL;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records/$DNSID");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");  
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        exit('Error: ' . curl_error($ch));
    }
    echo "The IP has changed from $old_ip to $ip!" . PHP_EOL;
}

domain.tld-updater.service (just call them after your domain so it’s easy to remember which one is which)

[Unit]
Description=Update domain.tld to current IP if needed

[Service]
Type=oneshot
ExecStart=/usr/bin/php /opt/domain.tld-updater.php

domain.tld-updater.timer (This will trigger the update command every minute)

[Unit]
Description=Update domain.tld entry in CloudFlare every minute

[Timer]
# Time to wait after booting before we run first time
OnBootSec=1min
# Time between running each consecutive time
OnUnitActiveSec=1m
Unit=domain.tld-updater.service

[Install]
WantedBy=timers.target

Don’t forget to enable the and start the timer with: sudo systemctl enable domain.tld-updater.timer and sudo systemctl start domain.tld-updater.timer.

On Windows just create a task in Task Scheduler which will run every minute (or 5).

If you see a error or fault in my script please tell me!


#2

Thank you @zjaakie