Connect DirectAdmin server to cPanel DNS cluster servers

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
If you're not keen on just rsyncing whole zone files or running other daemons etc, here's a script I wrote to connect a DA server to an existing cPanel DNS cluster. It still needs further work for things in the TODO list but it is functional.

In case there's some people that want to at least know this is easily possible if running mixed cPanel/DA servers and a cPanel cluster, it is.

Config file is separate for easier updating.

Code:
cd /usr/local/directadmin/scripts/custom
wget -O /usr/local/directadmin/scripts/custom/dns_write_post.sh https://files.netnerd.com/directadmin/dns_write_post.sh
wget -O /usr/local/directadmin/scripts/custom/dns_write_post_config.php https://files.netnerd.com/directadmin/dns_write_post_config.php.txt
chown diradmin:diradmin dns_write_post.sh && chown diradmin:diradmin dns_write_post_config.php
chmod 700 dns_write_post.sh && chmod 600 dns_write_post_config.php
Then edit dns_write_post_config.php to put in your cPanel server details, you can add as many as you like, but the checks only connect to the first one (to see if a domain exists, and get existing records, it's assumed that the DNS cluster records all match). Now creating or editing a dns zone in DA will push it to the cPanel cluster servers you defined.

Update: V2 ready, to update just run:

Code:
wget -O /usr/local/directadmin/scripts/custom/dns_write_post.sh https://files.netnerd.com/directadmin/dns_write_post.sh
The main change in v2 is that it now compares the zones first, and only deletes/adds new/changed records, which makes it a lot faster.

TODO:
- SOA record handling
- TTL's
 
Last edited:

chuvadenovembro

New member
Joined
Jul 1, 2019
Messages
4
If you're not keen on just rsyncing whole zone files or running other daemons etc, here's a script I wrote to connect a DA server to an existing cPanel DNS cluster. It still needs further work for things in the TODO list but it is functional.

In case there's some people that want to at least know this is easily possible if running mixed cPanel/DA servers and a cPanel cluster, it is.

Config file is separate for easier updating.

Code:
cd /usr/local/directadmin/scripts/custom
wget -O /usr/local/directadmin/scripts/custom/dns_write_post.sh https://files.netnerd.com/directadmin/dns_write_post.sh
wget -O /usr/local/directadmin/scripts/custom/dns_write_post_config.php https://files.netnerd.com/directadmin/dns_write_post_config.php.txt
chown diradmin:diradmin dns_write_post.sh && chown diradmin:diradmin dns_write_post_config.php
chmod 700 dns_write_post.sh && chmod 600 dns_write_post_config.php
Then edit dns_write_post_config.php to put in your cPanel server details, you can add as many as you like, but the checks only connect to the first one (to see if a domain exists, and get existing records, it's assumed that the DNS cluster records all match). Now creating or editing a dns zone in DA will push it to the cPanel cluster servers you defined.

Update: V2 ready, to update just run:

Code:
wget -O /usr/local/directadmin/scripts/custom/dns_write_post.sh https://files.netnerd.com/directadmin/dns_write_post.sh
The main change in v2 is that it now compares the zones first, and only deletes/adds new/changed records, which makes it a lot faster.

TODO:
- SOA record handling
- TTL's
H Nick,

Thank you for the great work.

I can not connect to my cpanel dns only

Please help me with this information.

$ns["ns1"] = array( "host" => "ns1.domain.com",'rootpw' => "rootpasswordhere");
$ns["ns2"] = array( "host" => "ns2.domain.com",'rootpw' => "rootpasswordhere");
["ns1"] = I did not change

"host" = I tested putting only domain, ip, dns primary

"ns1.domain.com" = I put my dns from the cluster

'rootpw' = I did not change

"rootpasswordhere" = Root password

I run .sh file I receive error message:

./dns_write_post.sh
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 39
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 40
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 56
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 57
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 73
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 74
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 86
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 87
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 88
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 98
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 99
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 109
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 110
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 121
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Undefined offset: 2 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Undefined offset: 3 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 242
PHP Notice: Array to string conversion in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 134
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 276
PHP Warning: array_reverse() expects parameter 1 to be array, null given in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 385
PHP Warning: Invalid argument supplied for foreach() in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 387
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
PHP Notice: Undefined index: host in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 210
thank you if you can help me
 

experttechit

Verified User
Joined
Jun 30, 2019
Messages
36
Thank You.

Will this work on a standalone DNS only server already running as NS2 for one of our servers?

also do we run the script on the dnsadmin server?
 

experttechit

Verified User
Joined
Jun 30, 2019
Messages
36
Hi Nick,

Sorry, I am struggeling to get it to work....

we just want it for ns2 only, so I filled in the ns2 details, but it doesn't seem to work?

[root@cloud01 custom]# ./dns_write_post.sh
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 39
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 40
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 56
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 57
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 73
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 74
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 86
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 87
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 88
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 98
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 99
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 109
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 110
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 121
PHP Notice: Undefined offset: 1 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Undefined offset: 2 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Undefined offset: 3 in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 123
PHP Notice: Array to string conversion in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 134
PHP Warning: array_reverse() expects parameter 1 to be array, null given in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 385
PHP Warning: Invalid argument supplied for foreach() in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 387

Thanks in advance
 

mitch2k

New member
Joined
Jul 2, 2019
Messages
1
Hi,

First of all thanks for the great work!

PHP Notice: Array to string conversion in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 134
PHP Warning: array_reverse() expects parameter 1 to be array, null given in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 385
PHP Warning: Invalid argument supplied for foreach() in /usr/local/directadmin/scripts/custom/dns_write_post.sh on line 387
same error here. It seems that the script isn't connecting to the cPanel API, and that's why the array is empty. However the log does not seem to contain any info.
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
Ok 2 things:

- If using only 1 nameserver, put it in the ns0 details, as the checks only use ns0.

- It sounds like you're also seeing a problem someone else had, in that directadmin isn't setting the environment variables as per

https://www.directadmin.com/features.php?id=450

Can you modify dns_write_post.sh so it just contains:

printenv >> envlog.txt

Then create a test A record, and post the contents of

cat /usr/local/directadmin/envlog.txt
 

experttechit

Verified User
Joined
Jun 30, 2019
Messages
36
Hi Nick,

thanks for your help....

- If using only 1 nameserver, put it in the ns0 details, as the checks only use ns0??

I can't see an ns0?, do we remove ns1,ns2 and just add ns0 ?

- It sounds like you're also seeing a problem someone else had, in that directadmin isn't setting the environment variables as per

https://www.directadmin.com/features.php?id=450

Can you modify dns_write_post.sh so it just contains:

printenv >> envlog.txt

Do we need to remove everything in this file and just add the line

printenv >> envlog.txt ?

Thanks again
 

hostixo

Verified User
Joined
Jun 30, 2019
Messages
11
Hello

I changed all this functions " $url = "

$url = "https://yourdnsonlyhostname:2087/json-api/".$command;

Now working well :)

Hi Nick,

thanks for your help....

- If using only 1 nameserver, put it in the ns0 details, as the checks only use ns0??

I can't see an ns0?, do we remove ns1,ns2 and just add ns0 ?

- It sounds like you're also seeing a problem someone else had, in that directadmin isn't setting the environment variables as per

https://www.directadmin.com/features.php?id=450

Can you modify dns_write_post.sh so it just contains:

printenv >> envlog.txt

Do we need to remove everything in this file and just add the line

printenv >> envlog.txt ?

Thanks again
 

hostixo

Verified User
Joined
Jun 30, 2019
Messages
11
But doesnt create dns zone.
Only update when we created dns zone manually.

Hello

I changed all this functions " $url = "

$url = "https://yourdnsonlyhostname:2087/json-api/".$command;

Now working well :)
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
Sorry if just using 1 nameserver, then you probably want to switch it to single curl, and delete ns2 line in config file.
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
But doesnt create dns zone.
Only update when we created dns zone manually.
This is the bit of code that should create the zone if it doesn't exist on the first nameserver, it's designed for that scenario:

Code:
// If zone doesn't exist, then create the default cpanel zone on cluster
if($zone_exists == false){
  $command = "adddns?domain=".$recs_domain."&ip=".$recs_domip;
  send_to_ns($command);
}
 

DirectAdmin Support

Administrator
Staff member
Joined
Feb 27, 2003
Messages
8,932
Without going into too much debugging of the 3rd party script, the dns_write_post.sh isn't a script you just "call" from ssh.
It must be called by DA when you do some action. Only then would all required ENV variables get filled.

Eg:
Admin Level -> DNS Administration -> domain.com -> add record / delete record, etc.

Hope that helps solves some confusion. Checking a sample box, there are lines like this:
//file_put_contents("/usr/local/directadmin/scripts/custom/cpdnslog.txt", "A records from DA: ".var_export($recs_a,true)."\n", FILE_APPEND);

which can be un-commented (remove the // characters from the far left side, and save the file), which then gives you more logging info about what the script is getting, inside the cpdnslog.txt.


John
 

Viperdh

New member
Joined
Jun 30, 2019
Messages
2
MX entry error

Hi,

Firstly thank you for making this script, apart from the MX record everything is working perfectly. It does error sometimes when updating but I that just seems more of a GUI timeout issue as everything still completes correctly, bar the MX record.

MX records are being updated to just 'mail.' not 'mail.domain.com.'

To fix update line 88
FROM:
$da_clean_arr[] = array('type' => 'MX', 'name' => $rec_arr[0], 'preference' => $rec2[0], 'exchange' => $rec2[1]);
TO:
$da_clean_arr[] = array('type' => 'MX', 'name' => $rec_arr[0], 'preference' => $rec2[0], 'exchange' => $rec2[1] . '.' . $recs_domain);

Thanks,
Daniel.
https://webhostingnz.com
 

Viperdh

New member
Joined
Jun 30, 2019
Messages
2
This is the bit of code that should create the zone if it doesn't exist on the first nameserver, it's designed for that scenario:

Code:
// If zone doesn't exist, then create the default cpanel zone on cluster
if($zone_exists == false){
  $command = "adddns?domain=".$recs_domain."&ip=".$recs_domip;
  send_to_ns($command);
}
I can confirm that it is not adding the zone file to the cpanel DNSONLY server. To test this I created an account on the Directadmin machine and checked for the file in /var/named on the DNSONLY, it was not present. I tried to reset the zonefile in DA to force it to copy to DNSONLY, it did not.

I have not had time to debug that bit of the code but I will in the next few days if a fix isn't posted for it.

Kind Regards,
Daniel.
https://webhostingnz.com
 

kang28ivan

Verified User
Joined
Mar 16, 2017
Messages
19
I can confirm that it is not adding the zone file to the cpanel DNSONLY server. To test this I created an account on the Directadmin machine and checked for the file in /var/named on the DNSONLY, it was not present. I tried to reset the zonefile in DA to force it to copy to DNSONLY, it did not.

I have not had time to debug that bit of the code but I will in the next few days if a fix isn't posted for it.

Kind Regards,
Daniel.
https://webhostingnz.com
yes, right. I am trying to make changes in dns_write_post_config.php it same can't write to DNS only cpanel
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
I can confirm that it is not adding the zone file to the cpanel DNSONLY server. To test this I created an account on the Directadmin machine and checked for the file in /var/named on the DNSONLY, it was not present. I tried to reset the zonefile in DA to force it to copy to DNSONLY, it did not.

I have not had time to debug that bit of the code but I will in the next few days if a fix isn't posted for it.

Kind Regards,
Daniel.
https://webhostingnz.com
Hmm I've tested that functionality numerous times and it does work on my test setup, along with the MX records you mentioned earlier. What version are your DNSonly servers? I'm wondering if there's something going on with the API calls that's behaving differently, you can see for yourself that the call is correct:

https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+adddns
 

experttechit

Verified User
Joined
Jun 30, 2019
Messages
36
Hi Nick,

we have the lastest version of cpaneldns only...

would it be better to create an api with password, rather than the server root password?
 

nick-a

Verified User
Joined
Feb 23, 2007
Messages
44
Rootpw vs api call doesn't make any difference, it's only the authentication method.

V3 will be out tonight with some improvements, checks all NS instead of just first once etc, and resets zones if existing ones don't match, and SOA record update.

I'm thinking for V4 it might be better to just parse the zone file directly, that way the script can support a CLI mode too without many alterations.
 

paulonichio

Verified User
Joined
Jul 8, 2019
Messages
5
Problems DKIM e MX

Hello, how are you?

I installed the script and it worked fine, but I noticed some problems.

The MX input is not actually sent automatically. If I enter DirectAdmin and only write the MX entry, it sends it correctly, but restoring a backup for example, it is not created.

I also noticed another problem in the DKIM key, it was created correctly in WHM DNS Only but with incorrect characters at the beginning, it looks like this:

"("v=DKIM1;
k=rsa;
p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvLcJZ+gMqecHCcoxr7RTfHO9gRNCghXbX7n8m6s2yAGiMM+gojO31zWEmD5RpLD4SYIt+jN/r8cdmDgL+WExFNw"\010\009\009\009\009\009"M/2KrlYfV8bgevv5HuBHxE8QFgWHqcb8THtrlrSjEWsWW0qs/tVivAXwTAy7GBZSSkgi/0Hfei4c/y867TQSltPzVRBZav5KTBNHk14TI6ZOAbcJ5SBqeSFtWxGP+Pc9hPXacqCx/W2AaufSHwMRIZ"\010\009\009\009\009\009"uZBJiKdD1i1HlaUWKp3luGO5ce/Gj6vEUHOM4QgF3CdpZQkZrmR1ZWAp4pKtGevua+lx+Wdn2RZ1hc2/Dcd63yqSlZexhQDoma34OuK7wIDAQAB")"

The correct one should be:

"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvLcJZ+gMqecHCcoxr7RTfHO9gRNCghXbX7n8m6s2yAGiMM+gojO31zWEmD5RpLD4SYIt+jN/r8cdmDgL+WExFNwM/2KrlYfV8bgevv5HuBHxE8QFgWHqcb8THtrlrSjEWsWW0qs/tVivAXwTAy7GBZSSkgi/0Hfei4c/y867TQSltPzVRBZav5KTBNHk14TI6ZOAbcJ5SBqeSFtWxGP+Pc9hPXacqCx/W2AaufSHwMRIZuZBJiKdD1i1HlaUWKp3luGO5ce/Gj6vEUHOM4QgF3CdpZQkZrmR1ZWAp4pKtGevua+lx+Wdn2RZ1hc2/Dcd63yqSlZexhQDoma34OuK7wIDAQAB"

Am I doing something wrong?

Thanks!
 
Top