Dynamic IP Auto Updater for Debian / Ubuntu ISPConfig 3 Server

Discussion in 'General' started by ahrasis, Apr 29, 2015.

  1. ahrasis

    ahrasis New Member

    Dynamic IP Auto Updater is designed and created especially for those who are using dynamic IP for their Debian / Ubuntu ISPConfig 3 Server instead of static IP as usually required. When it is setup using cron job as described in the how to guids below, the IP Updater will update the dynamic IP in ISPConfig 3 Server and resync it accordingly.

    You can simply use cut and past the code as shown below in your debian / ubuntu terminal to get ip_updater.php, ipu_resync.php and ipu_app.inc.php into /usr/local/ispconfig/interface/lib/ folder together with ip_updater for cron job, then, you are good to go.
    Code:
    cd /usr/local/ispconfig/interface/lib
    sudo wget https://raw.githubusercontent.com/ahrasis/IP_Updater/master/ip_updater.php
    sudo wget https://raw.githubusercontent.com/ahrasis/IP_Updater/master/ipu_resync.php
    sudo wget https://raw.githubusercontent.com/ahrasis/IP_Updater/master/ipu_app.inc.php
    cd /etc/cron.d
    sudo wget https://raw.githubusercontent.com/ahrasis/IP_Updater/master/ip_updater
    The how to guides are posted in its site http://ipupdater.sch.my. You can find the old, more detail guides at http://ipupdater.sch.my/oldways.

    The license is as stated in the ip_updater.php file and all other files are in its github. Feel free to fork the code and improvise it to suit your needs accordingly. The ip_updater.php code is appended below:
    Code:
    <?php
    /*
    Copyright (c) 2015, Ahmad Rasyid Ismail, [email protected]
    Project IP Updater for ubuntu, ispconfig 3 and dynamic ip users.
    BSD3 License. All rights reserved.
    
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
    may be used to endorse or promote products derived from this software without
    specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */
    
    /* Get database access by using ispconfig default configuration so no
    user and its password are disclosed. Exit if its connection failed */
    
    require_once 'config.inc.php';
    $ip_updater = mysqli_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_database']);
    if (mysqli_connect_errno()) {
    printf("\r\nConnection failed! \r\n\r\n", mysqli_connect_error());
    exit();
    }
    
    /* Else, it works. Now get public ip from a reliable source.
    We are using this but you can define your own. But We just
    need ipv4. So we exit if its filtering failed */
    
    $public_ip = file_get_contents('http://ip.sch.my/');
    if(!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
    printf("\r\nIPV4 for public IP filtering failed! \r\nYou may need to use other ip source. \r\n\r\n");
    exit();
    }
    
    /* Else, it's truly ipv4. Now we obtain the server ip,
    based on server id. Do change your server id accordingly. */
    
    $query_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
    list($db_ip) = mysqli_fetch_row($query_ip);
    
    /* Other than the above ip, we also need soa ip from bind files */
    
    $binds = glob('/etc/bind/pri.*');
    foreach ($binds as $bind)
    $filename[] = $bind;
    $matcher = file_get_contents($filename[0]);
    preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $matcher, $matched);
    $soa_ip = $matched[0];
    
    /* If the server public ip matches both, exit. Log is
    for error only, so we close database connection and restart
    apache without any logging for now on. However, you may
    enable it, by uncommenting the log line below this. */
    
    if(($public_ip == $db_ip) && ($public_ip == $soa_ip)) {
    // printf("\r\nThe server, soa zone files and public ip addresses match. \r\n\r\n");
    exit();
    }
    
    /* If not, start to change soa zone files with the new ip */
    
    if($public_ip != $soa_ip) {
    foreach ($binds as $bind) {
    $file = file_get_contents($bind);
    file_put_contents($bind, preg_replace("/$soa_ip/", "$public_ip", $file));
    }
    
    // Warn and update if soa zone files update failed.
    foreach(file($bind) as $binding=>$b) {
    if(strpos($b, "$soa_ip")==true) {
    printf("\r\nSOA zone files updates failed! \r\nZone files updating code may need a fix or update. \r\n\r\n");
    exit();
    }
    }
    }
    
    /* Then, we update our database with the new ip */
    
    if($public_ip != $db_ip) {
    $update1 = mysqli_query($ip_updater, "UPDATE dns_rr SET data = replace(data, '$db_ip', '$public_ip')");
    $update2 = mysqli_query($ip_updater, "UPDATE server_ip SET ip_address = replace(ip_address, '$db_ip', '$public_ip')");
    
    // Warn and exit if database update failed.
    $query_new_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
    list($db_new_ip) = mysqli_fetch_row($query_new_ip);
    if ($public_ip != $db_new_ip) {
    printf("\r\nDatabase updates failed! \r\nDatabase updating code may need a fix or update. \r\n\r\n");
    exit();
    }
    }
    
    /* Now resync so that above changes updated properly. Important!
    Do refer to http://ipupdater.sch.my and download or copy
    resync.php to ipu_resync,php and app.inc.php to
    ipu_app.inc.php. Disable admin check and tpl in ipu_resync.php
    and start_session() in ipu_app.inc.php and change require
    once in ipu_resync.php to ipu_app.inc.php. */
    
    require_once 'ipu_resync.php';
    
    /* Lastly, congratulations! All updates are successful. Log is
    for error only, so we close database connection and restart
    apache without any logging for now on. However, you may
    enable it, by uncommenting the line below this. */
    
    // printf("\r\nDatabase and SOA zone files updates are successful! \r\n\r\n");
    
    mysqli_close($ip_updater);
    exec('service apache2 restart');
    ?>
    
    I hope you will find these tips and tricks useful. Do report if there is any problem, thank you.
     
    Last edited: Sep 15, 2016
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    You better use the ispconfig remote api. The procedure that you use above will not result in a proper IP update for several reasons:

    1) Data in sys_datalog may never be modified. The data there is transaction data, so you can not modify a existing transaction.
    2) You dont update the serial of the zone, so your change will get ignored by all external dns servers and clients.

    To do a dynamic IP update corrrectly, use the ispconfig remote apt function dns_a_update.
     
  3. ahrasis

    ahrasis New Member

    Ok. So, I should not modify sys_datalog? But, I should update the serial and ip address of the zone instead? I can try to update the php script to conform with that.

    But using your advised method, how do I use dns_a_update.php? As I can see, it relates to a soap_config.php file.

    I did the following:
    1. Edit the soap_config.php with ispconfig cp admin login and password.
    2. Change soap_config.php ip address to localhost or local ip address.
    3. Upload both files to ispconfig web / server folder.
    4. Run dns_a_update.php in via ssh.
    5. I got SOAP error.

    Do I need to edit dns_a_update.php? Or can I used dns_a_edit.php?

    Thanks a million in advance.
     
  4. cypriot

    cypriot New Member

    Hi There,
    I would love to hear from you ahrasis , i am looking for kind of same thing but we want to create subdomains on our VPS and keep the ip updated same as our Home IP(dynamic) , would you be able to help please ,? i dont mind contributing
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    and which error do you got?
     
  6. ahrasis

    ahrasis New Member

    @cypriot I am playing with it. The latest one is in the github. I use a modified resync.php and modified app.inc.pho.

    @till I will test it again and report the error with better details. My guess is may be because I ssh from localhost.
     
  7. cypriot

    cypriot New Member

    @ahrasis
    Thank you for your reply, do you mean it is okay to use for subdomains as it is to update their IP record as the local IP changes at home server ?
     
  8. ahrasis

    ahrasis New Member

    Yes. It should work just fine as I am using one myself. The only hiccup will be the time for dns propagation which is unavoidable in most cases.
     
  9. ahrasis

    ahrasis New Member

    Updated to compliment php5.6 and php7.0 (previously php 5.5). Feel free to comment, report any errors or improvise further.
     
  10. sjau

    sjau Local Meanie Moderator

    I wrote my own little script a while back but that seems a lot better. When I need it for something new I'll probably use yours.
     
    ahrasis likes this.
  11. ahrasis

    ahrasis New Member

    Updated as ip_updater.php sometime miss updating soa zone files. Do report if you are facing errors with the new one.
     

Share This Page