ISPConfig 3 Update a DNS 'A' Record programmatically

Discussion in 'General' started by sghazagh, Sep 6, 2019.

  1. dan-v

    dan-v Member

    Got it, thx. I am almost there.

    Two additional questions
    1. what is the definition/use of the $client_id in these function calls ? is it the number of the client space i.e. 0 for single-client systems ? Or where is that documented ?

    2. (perhaps a naive question) Is there a way ot get the Id of a DNS record from it's contents, other than by brute force scanning ? e.g. by querying the database
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    It's the ID of the client. See lists of clients, there is a colum named ID. If you set client_id = 0, then the record belongs to the admin and not to a client.

    You can use any *_get() function of the API to search for records. Example:

    Code:
    $dns_zone = $client->dns_zone_get($session_id, array('origin' => 'yourdomain.tld.'));
     
  3. dan-v

    dan-v Member

    Thx Till, your help has been very effective (and patient, too). I have developed a pure PHP script that will update the IP of an A record designated by its URL. I am posting it below for the benefit of those interested.
    PHP:
    #!/usr/bin/php
    <?php

    #
    # This script will change the IP address of an "A" record in an ISPConfig DNS zone, accessing the record by its name (URL)
    #
    # Note 1 : It is assumed that the existing A record belongs to the administrator and is unique, otherwise the sript will exit in error
    # Note 2 : For this script to execute properly, it is necessary :
    #
    #   a.  to have installed the PHP SOAP extension
    #   b.  using the ISPConfig SYSTEM tab, to have created a Remote User with the "DNS a functions" enabled (use strong password)
    #
    #

    // --------- This is the part requiring customization for your system and user data ------------

    $soap_location  'https://yourserver.yourdomain.com:8080/remote/index.php';
    $soap_uri       'https://yourserver.yourdomain.com:8080/remote/';
    $username       'Your_remote_user_ID';
    $password       'Your_remote_user_password';

    // ------------------------------ End of customization section ----------------------------------

    $Script_name $argv[0];
    function 
    usage($message)
    {
        global 
    $Script_name;

        echo 
    "Error: $message\n";
        echo 
    "usage: $Script_name URL Ipv4_address\n";
        exit(
    1);
    }

    if (
    count($argv) != 3usage("Invalid number of arguments");
    $My_URL     $argv[1];
    $My_new_IP  $argv[2];

    # Parameters check

    if (! filter_var($My_new_IPFILTER_VALIDATE_IPFILTER_FLAG_IPV4)) usage("<$My_new_IP> is not a valid IPv4 address");

    # Fix potential absence of trailing dot in supplied URL

    $My_A_Record preg_replace("/\.\.$/"".""$My_URL.");

    $client = new SoapClient(null, array(
                    
    'location' => $soap_location,
                    
    'uri'      => $soap_uri,
                    
    'trace' => 1,
                    
    'exceptions' => 1
                    
    ));

    try
    {
        if (! 
    $session_id $client->login($username$password))
        {
            echo 
    "Error: Could not log in for soap operations\n";
            exit(
    1);
        }
        else
        
    #    echo "Logged successfully. Session ID: $session_id\n";             # For DEBUG purposes

        
    $dns_record $client->dns_a_get($session_id, array('name' => $My_A_Record'type' => 'A'));

        if (
    count($dns_record) != 1)
        {
            
    $client->logout($session_id);
            echo 
    "Multiple or no record(s) found for $My_URL\n";
            exit(
    2);
        }

        
    $dns_record $dns_record[0];
        
    $id $dns_record['id'];

        
    # print_r($dns_record);                                                 # for DEBUG purposes.

        #
        # At this point $id has the number of our A record and it is loaded in $dns_record
        # Now we change the address and update the record
        # Note the optional TRUE parameter at the end of the call, so that ISPConfig updates the serial
        #

        
    $Old_IP $dns_record['data'];
        if (
    "$Old_IP== "$My_new_IP")
        {
            
    // There is no change from the old IP => do nothing

            
    $client->logout($session_id);
            exit(
    0);
        }

        
    $client_id 0;                                                       # Client is admin, here
        
    $dns_record['data'] = $My_new_IP;


        
    $affected_rows $client->dns_a_update($session_id$client_id$id$dns_recordtrue);

        if (
    $affected_rows == 0)
        {
            
    $client->logout($session_id);
            echo 
    "Modification failed. Ip address of $My_URL remains $Old_IP.\n";
            exit(
    1);
        }

        echo 
    "IP address of $My_URL changed from $Old_IP to $My_new_IP in $affected_rows location(s)\n";

        
    $client->logout($session_id);
        exit(
    0);
    }

    catch (
    SoapFault $e)
    {
        echo 
    "$client->__getLastResponse()\n";
        die(
    "SOAP Error: $e->getMessage()\n");
    }

     
    Last edited: Mar 9, 2026 at 1:43 AM
  4. dan-v

    dan-v Member

    Till, If I may make a suggestion, it would be great if the ISPCongfig panel did this to the URLs which are input when creating or updating a DNS record:

    # Fix potential absence of trailing dot in supplied URL
    $My_A_Record = preg_replace("/\.\.$/", ".", "$My_URL.");

    It would improve significantly the panel user experience!
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    A-Records do not have to end with a dot, it's perfectly valid to use the short form like 'www' instead of 'www.somedomain.tld.', which is without a dot. Therefore, you can not add a dot automatically.
     

Share This Page