AriyankiNet        
 


CodeworkingPHPArticle ≫ How to make your own Dynamic DNS Server with CPANEL API 2

How to make your own Dynamic DNS Server with CPANEL API 2

Wed 16 Sep 2015, 06:59


Usually ISP (Internet Service Providers) assign you a different IP-address each time you connect to the net via modem, ISDN or xDSL. 

if you want to have an internet domain name (or URL) to go with that IP-address, you have to configure that IP-address with Dynamic DNS.

Read this about Dynamic DNS https://en.wikipedia.org/wiki/Dynamic_DNS

How does it work? Your server contacts the Dynamic DNS server at regular intervals (or whenever its necessary), to report its current IP number and update the mapping.

If you have a web hosting, you have permission to change your dns zone, and your web hosting have API, you can make your own dynamic dns server with your internet domain name.

In my case, my web hosting use cpanel version 2, this is the API documentation from panel https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2

This is my step to do it:

  • I made php code to hit that cpanel API.
  • Put that code in my web hosting. 
  • Go to Advanced DNS Zone Editor in my webhosting cpanel and Add Record DNS Zone for initiation: (i don't include add DNS Zone event in my code to reduce the process)
    • Name:..<com/net/id/org/etc>
    • TTL:7200
    • Type:A
    • Address:
  • From my server at home (in this case i am using wifi router tp-link mr3420 with openwrt os) i hit that code with scheduler every 10 minutes.

 

 

This is my scheduler script:

10 * * * * /usr/bin/wget -q "http:///.php”

Download the code on my github https://github.com/ariyanki/ddns-cpanel

(you can download my code from http://www.ariyanki.net/download/)

And this is my php code: 

 

$cpanelUrl="www.cpanel.com";
$cpanelPort="2083";
$cpanelUser="username";
$cpanelPassword="password";
$domain="yourdomain.com";
$subdomain="subdomain";
$newIP=$_SERVER['REMOTE_ADDR'];
$output=updateddns();
//print_r($output);

function updateddns(){
	global $cpanelUrl,$cpanelPort,$cpanelUser,$cpanelPassword,$domain,$subdomain,$newIP;
	$records = doquery("fetchzone", array());
	$line="";
	foreach ($records[cpanelresult][data][0][record] as $val) {
		if(preg_match('/'.$subdomain.'.'.$domain.'/',$val["name"])){
			$line=$val['Line'];
			$ip=$val['record'];
			break;

		}
	}
	if(!empty($line) and !empty($newIP) and $ip!=$newIP){
		$params = array(	'Line' => $line,
						"type"		=> "A",
						"name"		=> $subdomain,
						"address"	=> $newIP,
						"ttl"		=> "7200",
						"class"		=> "IN");
		$records = doquery("edit_zone_record",$params);
		echo "success";
	}else{
		echo "failed.";
	}
	return $records;
}

function doquery($function,$params){
	global $cpanelUrl,$cpanelPort,$cpanelUser,$cpanelPassword,$domain;
	$curl = curl_init();
	$query = "https://".$cpanelUrl.":".$cpanelPort."/json-api/cpanel?cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=".$function."&cpanel_jsonapi_version=2&domain=".$domain."&".http_build_query($params);
	$headers[] = "Authorization: Basic ".base64_encode($cpanelUser.":".$cpanelPassword);
	$options = array(	CURLOPT_URL				=> $query,
	CURLOPT_SSL_VERIFYPEER 	=> 0,		//Allow self-signed cert :P
	CURLOPT_SSL_VERIFYHOST 	=> 0,		//Allow cert hostname mismatch
	CURLOPT_HEADER			=> 0,		//Output: Header not included
	CURLOPT_RETURNTRANSFER	=> 1,		//Output: Contents included
	CURLOPT_HTTPHEADER		=> $headers	//Auth
	);
	curl_setopt_array($curl, $options);
	$result = curl_exec($curl);

	if ($result === false) { throw new Exception("cURL Execution Error ".curl_error($curl)." in $query", 0); } //error handling for failure
	curl_close($curl);
	return json_decode($result, true);
}

2 12503 views