Last active
October 29, 2017 08:06
-
-
Save utoxin/3898671ddac600188521 to your computer and use it in GitHub Desktop.
Recursive Authoritative Nameserver Lookup in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NSLookup { | |
private $root_servers = array( | |
'a.root-servers.net.', | |
'b.root-servers.net.', | |
'c.root-servers.net.', | |
'd.root-servers.net.', | |
'e.root-servers.net.', | |
'f.root-servers.net.', | |
'g.root-servers.net.', | |
'h.root-servers.net.', | |
'i.root-servers.net.', | |
'j.root-servers.net.', | |
'k.root-servers.net.', | |
'l.root-servers.net.', | |
'm.root-servers.net.', | |
); | |
private function get_next_level($domain, $nameservers = null) { | |
if ($nameservers == null) { | |
$nameservers = $this->root_servers; | |
} | |
shuffle($nameservers); | |
$cli_domain = escapeshellarg($domain); | |
$found = false; | |
$found_nameservers = array(); | |
while ($nameserver = escapeshellarg(array_shift($nameservers))) { | |
exec("dig +nocomments +noadditional +nocmd +nostats NS {$cli_domain} @{$nameserver}", $output, $status); | |
if ($status != 0) { | |
continue; | |
} | |
foreach ($output AS $line) { | |
if (preg_match("/^(\S+)\s+\d+\s+IN\s+NS\s+(\S+)$/", $line, $matches)) { | |
if ($matches[1] == $domain.'.') { | |
$found = true; | |
} | |
$found_nameservers[] = $matches[2]; | |
} | |
} | |
break; | |
} | |
return array( | |
'found' => $found, | |
'nameservers' => $found_nameservers | |
); | |
} | |
public function get_nameservers($domain) { | |
$result = $this->get_next_level($domain); | |
while ($result['found'] == false && count($result['nameservers'])) { | |
$result = $this->get_next_level($domain, $result['nameservers']); | |
} | |
return $result['found'] ? $result['nameservers'] : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment