Mikrotik export dns to zonefile
Jump to navigation
Jump to search
<?php
$DEBUG = false;
$ip = "192.168.1.1";
$user = "apiuser";
$password = "password";
require('routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = $DEBUG;
if ($API->connect($ip, $user, $password)) {
$ARRAY = $API->comm("/ip/dns/static/print");
usort($ARRAY, function ($a, $b) {
// Split the domain into the main part and suffix
$partsA = explode('.', $a['name']);
$partsB = explode('.', $b['name']);
// Extract suffix (everything after the first dot)
$suffixA = implode('.', array_slice($partsA, 1));
$suffixB = implode('.', array_slice($partsB, 1));
// If suffixes are the same, sort by the full name
if ($suffixA === $suffixB) {
return strcmp($a['name'], $b['name']);
}
// Sort by suffix alphabetically
return strcmp($suffixA, $suffixB);
});
$count = 0;
foreach ($ARRAY as $dns) {
$count++;
if ($dns["disabled"] == "true") continue;
$type = "A";
if (isset($dns["type"])) $type = $dns["type"];
$address = "";
if ($type !== "A") {
$address = $dns[strtolower($type)];
} else {
$address = $dns["address"];
}
echo $dns["name"]," IN ".$type." ".$address."<br>";
}
echo "Total entries: $count<br>";
$API->disconnect();
} else {
echo "Unable to connect to the router";
}
?>