|
8 | 8 | outputStdout("This script is not affiliated with netcup.");
|
9 | 9 | outputStdout("=============================================\n");
|
10 | 10 |
|
| 11 | +outputStdout(sprintf("Updating DNS records for host %s on domain %s\n", HOST, DOMAIN)); |
| 12 | + |
11 | 13 | // Login
|
12 | 14 | if ($apisessionid = login(CUSTOMERNR, APIKEY, APIPASSWORD)) {
|
13 | 15 | outputStdout("Logged in successfully!");
|
|
45 | 47 | }
|
46 | 48 |
|
47 | 49 | //Find the host defined in config.php
|
48 |
| -$foundHosts = array(); |
| 50 | +$foundHostsV4 = array(); |
49 | 51 |
|
50 | 52 | foreach ($infoDnsRecords['responsedata']['dnsrecords'] as $record) {
|
51 | 53 | if ($record['hostname'] === HOST && $record['type'] === "A") {
|
52 |
| - $foundHosts[] = array( |
| 54 | + $foundHostsV4[] = array( |
53 | 55 | 'id' => $record['id'],
|
54 | 56 | 'hostname' => $record['hostname'],
|
55 | 57 | 'type' => $record['type'],
|
|
62 | 64 | }
|
63 | 65 |
|
64 | 66 | //If we can't find the host, create it.
|
65 |
| -if (count($foundHosts) === 0) { |
66 |
| - outputStdout(sprintf("Host %s doesn't exist, creating necessary DNS-Record.", HOST)); |
67 |
| - $foundHosts[] = array( |
| 67 | +if (count($foundHostsV4) === 0) { |
| 68 | + outputStdout(sprintf("A record for host %s doesn't exist, creating necessary DNS record.", HOST)); |
| 69 | + $foundHostsV4[] = array( |
68 | 70 | 'hostname' => HOST,
|
69 | 71 | 'type' => 'A',
|
70 | 72 | 'destination' => 'newly created Record',
|
71 | 73 | );
|
72 | 74 | }
|
73 | 75 |
|
74 | 76 | //If the host with A record exists more than one time...
|
75 |
| -if (count($foundHosts) > 1) { |
76 |
| - outputStderr(sprintf("Found multiple A-Records for the Host %s – Please specify a host for which only a single A-Record exists in config.php. Exiting.", HOST)); |
| 77 | +if (count($foundHostsV4) > 1) { |
| 78 | + outputStderr(sprintf("Found multiple A records for the host %s – Please specify a host for which only a single A record exists in config.php. Exiting.", HOST)); |
77 | 79 | exit(1);
|
78 | 80 | }
|
79 | 81 |
|
80 | 82 | //If we couldn't determine a valid public IPv4 address
|
81 |
| -if (!$publicIP = getCurrentPublicIPv4()) { |
| 83 | +if (!$publicIPv4 = getCurrentPublicIPv4()) { |
82 | 84 | outputStderr("Main API and fallback API didn't return a valid IPv4 address. Exiting.");
|
83 | 85 | exit(1);
|
84 | 86 | }
|
85 | 87 |
|
86 |
| -$ipchange = false; |
| 88 | +$ipv4change = false; |
87 | 89 |
|
88 | 90 | //Has the IP changed?
|
89 |
| -foreach ($foundHosts as $record) { |
90 |
| - if ($record['destination'] !== $publicIP) { |
| 91 | +foreach ($foundHostsV4 as $record) { |
| 92 | + if ($record['destination'] !== $publicIPv4) { |
91 | 93 | //Yes, it has changed.
|
92 |
| - $ipchange = true; |
93 |
| - outputStdout(sprintf("IP has changed. Before: %s; Now: %s", $record['destination'], $publicIP)); |
| 94 | + $ipv4change = true; |
| 95 | + outputStdout(sprintf("IPv4 address has changed. Before: %s; Now: %s", $record['destination'], $publicIPv4)); |
94 | 96 | } else {
|
95 | 97 | //No, it hasn't changed.
|
96 |
| - outputStdout("IP hasn't changed. Current IP: ".$publicIP.""); |
| 98 | + outputStdout("IPv4 address hasn't changed. Current IPv4 address: ".$publicIPv4); |
97 | 99 | }
|
98 | 100 | }
|
99 | 101 |
|
100 | 102 | //Yes, it has changed.
|
101 |
| -if ($ipchange === true) { |
102 |
| - $foundHosts[0]['destination'] = $publicIP; |
| 103 | +if ($ipv4change === true) { |
| 104 | + $foundHostsV4[0]['destination'] = $publicIPv4; |
103 | 105 | //Update the record
|
104 |
| - if (updateDnsRecords(DOMAIN, CUSTOMERNR, APIKEY, $apisessionid, $foundHosts)) { |
105 |
| - outputStdout("IP address updated successfully!"); |
| 106 | + if (updateDnsRecords(DOMAIN, CUSTOMERNR, APIKEY, $apisessionid, $foundHostsV4)) { |
| 107 | + outputStdout("IPv4 address updated successfully!"); |
106 | 108 | } else {
|
107 | 109 | exit(1);
|
108 | 110 | }
|
109 | 111 | }
|
110 | 112 |
|
| 113 | +if (USE_IPV6 === true) { |
| 114 | + //Find the host defined in config.php |
| 115 | + $foundHostsV6 = array(); |
| 116 | + |
| 117 | + foreach ($infoDnsRecords['responsedata']['dnsrecords'] as $record) { |
| 118 | + if ($record['hostname'] === HOST && $record['type'] === "AAAA") { |
| 119 | + $foundHostsV6[] = array( |
| 120 | + 'id' => $record['id'], |
| 121 | + 'hostname' => $record['hostname'], |
| 122 | + 'type' => $record['type'], |
| 123 | + 'priority' => $record['priority'], |
| 124 | + 'destination' => $record['destination'], |
| 125 | + 'deleterecord' => $record['deleterecord'], |
| 126 | + 'state' => $record['state'], |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + //If we can't find the host, create it. |
| 132 | + if (count($foundHostsV6) === 0) { |
| 133 | + outputStdout(sprintf("AAAA record for host %s doesn't exist, creating necessary DNS record.", HOST)); |
| 134 | + $foundHostsV6[] = array( |
| 135 | + 'hostname' => HOST, |
| 136 | + 'type' => 'AAAA', |
| 137 | + 'destination' => 'newly created Record', |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + //If the host with AAAA record exists more than one time... |
| 142 | + if (count($foundHostsV6) > 1) { |
| 143 | + outputStderr(sprintf("Found multiple AAAA records for the host %s – Please specify a host for which only a single AAAA record exists in config.php. Exiting.", HOST)); |
| 144 | + exit(1); |
| 145 | + } |
| 146 | + |
| 147 | + //If we couldn't determine a valid public IPv6 address |
| 148 | + if (!$publicIPv6 = getCurrentPublicIPv6()) { |
| 149 | + outputStderr("Main API and fallback API didn't return a valid IPv6 address. Do you have IPv6 connectivity? If not, please disable USE_IPV6 in config.php. Exiting."); |
| 150 | + exit(1); |
| 151 | + } |
| 152 | + |
| 153 | + $ipv6change = false; |
| 154 | + |
| 155 | + //Has the IP changed? |
| 156 | + foreach ($foundHostsV6 as $record) { |
| 157 | + if ($record['destination'] !== $publicIPv6) { |
| 158 | + //Yes, it has changed. |
| 159 | + $ipv6change = true; |
| 160 | + outputStdout(sprintf("IPv6 address has changed. Before: %s; Now: %s", $record['destination'], $publicIPv6)); |
| 161 | + } else { |
| 162 | + //No, it hasn't changed. |
| 163 | + outputStdout("IPv6 address hasn't changed. Current IPv6 address: ".$publicIPv6); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + //Yes, it has changed. |
| 168 | + if ($ipv6change === true) { |
| 169 | + $foundHostsV6[0]['destination'] = $publicIPv6; |
| 170 | + //Update the record |
| 171 | + if (updateDnsRecords(DOMAIN, CUSTOMERNR, APIKEY, $apisessionid, $foundHostsV6)) { |
| 172 | + outputStdout("IPv6 address updated successfully!"); |
| 173 | + } else { |
| 174 | + exit(1); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
111 | 179 | //Logout
|
112 | 180 | if (logout(CUSTOMERNR, APIKEY, $apisessionid)) {
|
113 | 181 | outputStdout("Logged out successfully!");
|
|
0 commit comments