Skip to content

Commit 16b399d

Browse files
committed
First commit
0 parents  commit 16b399d

File tree

4 files changed

+416
-0
lines changed

4 files changed

+416
-0
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Dynamic DNS client for netcup DNS API
2+
*This project is not affiliated with the company netcup GmbH. Although it is developed by an employee, it is not an official client by netcup GmbH and was developed in my free time.*
3+
*netcup is a registered trademark of netcup GmbH, Karlsruhe, Germany.*
4+
5+
**A simple dynamic DNS client written in PHP for use with the netcup domain API.**
6+
7+
## Requirements
8+
* Be a netcup customer: https://www.netcup.de – or for international customers: https://www.netcup.eu
9+
* You don't have to be a domain reseller to use the necessary functions for this client – every customer with a domain may use it.
10+
* netcup API key and API password, which can be created within your CCP at https://ccp.netcup.net
11+
* PHP-CLI with CURL extension
12+
* A domain :wink:
13+
14+
## Features
15+
### Implemented
16+
* All necessary API functions for DNS actions implemented (REST API)
17+
* Determines correct public IP address
18+
* Updating of a specific subdomain, domain root, or subdomain
19+
* If configured, lowers TTL to 300 seconds for the domain on each run, if necessary
20+
21+
### Missing
22+
* Support for domain root and wildcard / specific subdomains at the same time
23+
* Creation of DNS record, if it doesn't already exist
24+
* Hiding output (quiet option)
25+
* Caching the IP provided to netcup DNS, to avoid running into (currently not existing) rate limits in the domain API
26+
* Add fallback API for determining public IP address, in case main API does return invalid / no IP address
27+
* Probably a lot more :grin: – to be continued...
28+
29+
## Getting started
30+
### Configuration
31+
Configuration is very simple: Just fill out `config.php` with the required values.
32+
33+
### How to use
34+
`php update.php`
35+
36+
You should probably run this script every few minutes, so that your IP is updated as quickly as possible. Add it to your cronjobs and run it regularly, for example every five minutes.
37+
38+
## Example output
39+
```
40+
$ php update.php
41+
===============================================================
42+
Running dynamic DNS client for netcup 1.0 at 2018/05/30 01:14:43
43+
This script is not affiliated with netcup.
44+
===============================================================
45+
46+
Logged in successfully!
47+
48+
Successfully received Domain info.
49+
50+
Lowered TTL to 300 seconds successfully.
51+
52+
Successfully received DNS record data.
53+
54+
IP has changed
55+
Before: 1.2.3.4
56+
Now: 5.6.7.8
57+
58+
IP address updated successfully!
59+
60+
Logged out successfully!
61+
62+
63+
```
64+
```
65+
$ php update.php
66+
===============================================================
67+
Running dynamic DNS client for netcup 1.0 at 2018/05/30 01:19:43
68+
This script is not affiliated with netcup.
69+
===============================================================
70+
71+
Logged in successfully!
72+
73+
Successfully received Domain info.
74+
75+
Successfully received DNS record data.
76+
77+
IP hasn't changed. Current IP: 5.6.7.8
78+
79+
Logged out successfully!
80+
81+
82+
```
83+
84+
If you have ideas on how to improve this script, please don't hesitate to create an issue or provide me with a pull request. Thank you!

config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// Enter your netcup customer number here
3+
define("CUSTOMERNR", "12345");
4+
5+
//Enter your API-Key and -Password here - you can generate them in your CCP at https://ccp.netcup.net
6+
define("APIKEY", "abcdefghijklmnopqrstuvwxyz");
7+
define("APIPASSWORD", "abcdefghijklmnopqrstuvwxyz");
8+
9+
// Enter Domain which should be used for dynamic DNS
10+
define("DOMAIN","mydomain.com");
11+
//Enter subdomain to be used for dynamic DNS, alternatively "@" for domain root or "*" for wildcard. Currently, the DNS record should already exist before running the script (it can't create the record yet)
12+
define("HOST", "server");
13+
14+
//If set to True, this will change TTL to 300 seconds on every run if necessary.
15+
define("CHANGE_TTL", True);
16+
17+
// Use netcup DNS REST-API
18+
define("APIURL", "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON");

functions.php

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<?php
2+
3+
require_once("config.php");
4+
5+
// Sends $request to netcup Domain API and returns the result
6+
function sendRequest($request) {
7+
$ch = curl_init(APIURL);
8+
$curlOptions = array(
9+
CURLOPT_POST => 1,
10+
CURLOPT_RETURNTRANSFER => 1,
11+
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
12+
CURLOPT_POSTFIELDS => $request
13+
);
14+
curl_setopt_array($ch, $curlOptions);
15+
16+
$result = curl_exec($ch);
17+
curl_close($ch);
18+
19+
$result = json_decode($result, True);
20+
return $result;
21+
}
22+
23+
//Returns current public IP.
24+
function getCurrentPublicIP() {
25+
$publicIP = file_get_contents('https://api.ipify.org');
26+
27+
//Let's check that this is really a IPv4 address, just in case...
28+
if (filter_var($publicIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
29+
return($publicIP);
30+
}
31+
else {
32+
echo("https://api.ipify.org didn't return a valid IPv4 address.\n\n");
33+
return(False);
34+
}
35+
}
36+
37+
//Login into netcup domain API and returns Apisessionid
38+
function login($customernr, $apikey, $apipassword) {
39+
40+
$logindata = array(
41+
'action' => 'login',
42+
'param' =>
43+
array(
44+
'customernumber' => CUSTOMERNR,
45+
'apikey' => APIKEY,
46+
'apipassword' => APIPASSWORD
47+
)
48+
);
49+
50+
$request = json_encode($logindata);
51+
52+
$result = sendRequest($request);
53+
54+
if ($result['status'] == "success") {
55+
return($result['responsedata']['apisessionid']);
56+
}
57+
else {
58+
echo("ERROR: Error while logging in: " . $result['longmessage'] . "\n\n");
59+
return(False);
60+
}
61+
}
62+
//Logout of netcup domain API, returns boolean
63+
function logout($customernr, $apikey, $apisessionid) {
64+
65+
$logoutdata = array(
66+
'action' => 'logout',
67+
'param' =>
68+
array(
69+
'customernumber' => CUSTOMERNR,
70+
'apikey' => APIKEY,
71+
'apisessionid' => $apisessionid
72+
)
73+
);
74+
75+
$request = json_encode($logoutdata);
76+
77+
$result = sendRequest($request);
78+
79+
if ($result['status'] == "success") {
80+
return(True);
81+
}
82+
else {
83+
echo("ERROR: Error while logging out: " . $result['longmessage'] . "\n\n");
84+
return(False);
85+
}
86+
}
87+
//Get info about dns zone from netcup domain API, returns result
88+
function infoDnsZone($domainname, $customernr, $apikey, $apisessionid) {
89+
$infoDnsZoneData = array(
90+
'action' => 'infoDnsZone',
91+
'param' =>
92+
array(
93+
'domainname' => $domainname,
94+
'customernumber' => $customernr,
95+
'apikey' => $apikey,
96+
'apisessionid' => $apisessionid
97+
)
98+
);
99+
100+
$request = json_encode($infoDnsZoneData);
101+
102+
$result = sendRequest($request);
103+
104+
if ($result['status'] == "success") {
105+
return($result);
106+
}
107+
else {
108+
echo("Error while getting DNS Zone info: " . $result['longmessage'] . "\n\n");
109+
return(False);
110+
}
111+
}
112+
113+
//Get info about dns records from netcup domain API, returns result
114+
function infoDnsRecords($domainname, $customernr, $apikey, $apisessionid) {
115+
$infoDnsRecordsData = array(
116+
'action' => 'infoDnsRecords',
117+
'param' =>
118+
array(
119+
'domainname' => $domainname,
120+
'customernumber' => $customernr,
121+
'apikey' => $apikey,
122+
'apisessionid' => $apisessionid
123+
)
124+
);
125+
126+
$request = json_encode($infoDnsRecordsData);
127+
128+
$result = sendRequest($request);
129+
130+
if ($result['status'] == "success") {
131+
return($result);
132+
}
133+
else {
134+
echo("Error while getting DNS Record info: " . $result['longmessage'] . "\n\n");
135+
return(False);
136+
}
137+
}
138+
139+
//Updates DNS Zone using the netcup domain API and returns boolean
140+
function updateDnsZone($domainname, $customernr, $apikey, $apisessionid, $dnszone) {
141+
$updateDnsZoneData = array(
142+
'action' => 'updateDnsZone',
143+
'param' =>
144+
array(
145+
'domainname' => $domainname,
146+
'customernumber' => $customernr,
147+
'apikey' => $apikey,
148+
'apisessionid' => $apisessionid,
149+
'dnszone' => $dnszone
150+
)
151+
);
152+
153+
$request = json_encode($updateDnsZoneData);
154+
155+
$result = sendRequest($request);
156+
157+
if ($result['status'] == "success") {
158+
return(True);
159+
}
160+
else {
161+
echo("Error while updating DNS Zone: " . $result['longmessage'] . "\n\n");
162+
return(False);
163+
}
164+
}
165+
166+
//Updates DNS records using the netcup domain API and returns boolean
167+
function updateDnsRecords($domainname, $customernr, $apikey, $apisessionid, $dnsrecords) {
168+
$updateDnsZoneData = array(
169+
'action' => 'updateDnsRecords',
170+
'param' =>
171+
array(
172+
'domainname' => $domainname,
173+
'customernumber' => $customernr,
174+
'apikey' => $apikey,
175+
'apisessionid' => $apisessionid,
176+
'dnsrecordset' => array(
177+
'dnsrecords' => $dnsrecords
178+
)
179+
)
180+
);
181+
182+
$request = json_encode($updateDnsZoneData);
183+
184+
$result = sendRequest($request);
185+
186+
if ($result['status'] == "success") {
187+
return(True);
188+
}
189+
else {
190+
echo("Error while updating DNS Records: " . $result['longmessage'] . "\n\n");
191+
return(False);
192+
}
193+
}

0 commit comments

Comments
 (0)