Last active
September 23, 2016 00:50
-
-
Save rosswintle/904f38e2345d7cc6a0ad7c783eeec604 to your computer and use it in GitHub Desktop.
Domain Info script - attempts to give you registrar nameservers, basic DNS, actual hostname hosting the domain and MX records
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 | |
/* | |
* This needs comments and command-line arguments and stuff. It's ruff and ready. | |
*/ | |
global $domain_re; | |
$domain_re = '([0-9a-zA-Z\-]+\.)+[0-9a-zA-Z\-]+'; | |
function get_nameservers( $whois_out, $domain ) { | |
if ('.uk' == substr($domain, -3)) { | |
return get_uk_nameservers( $whois_out ); | |
} else { | |
return get_nonuk_nameservers( $whois_out ); | |
} | |
} | |
function get_registrar( $whois_out, $domain ) { | |
if ('.uk' == substr($domain, -3)) { | |
return get_uk_registrar( $whois_out ); | |
} else { | |
return get_nonuk_registrar( $whois_out ); | |
} | |
} | |
function get_uk_registrar( $whois_out ) { | |
global $domain_re; | |
$found_intro = false; | |
$no_more = false; | |
$registrar_lines = []; | |
foreach ( $whois_out as $line ) { | |
if (! $no_more) { | |
if ( $found_intro ) { | |
if (!empty($line)) { | |
$registrar_lines[] = trim($line); | |
} else { | |
$no_more = true; | |
} | |
} | |
else if ( false !== strpos( $line, "Registrar:" ) ) { | |
$found_intro = true; | |
} | |
} | |
} | |
return $registrar_lines; | |
} | |
function get_nonuk_registrar( $whois_out ) { | |
global $domain_re; | |
$registrar_lines = []; | |
foreach( $whois_out as $line ) { | |
if (preg_match( '/Sponsoring Registrar:\s+(.*)$/', $line, $matches ) ) { | |
$registrar_lines[] = $matches[1]; | |
} | |
} | |
return $registrar_lines; | |
} | |
function get_uk_nameservers( $whois_out ) { | |
global $domain_re; | |
$found_intro = false; | |
$no_more = false; | |
$servers = []; | |
foreach ( $whois_out as $line ) { | |
if (! $no_more) { | |
if ( $found_intro ) { | |
if (preg_match( '/(' . $domain_re . ')/', $line, $matches ) ) { | |
$servers[] = $matches[0]; | |
} else { | |
$no_more = true; | |
} | |
} | |
else if ( false !== strpos( $line, "Name servers:" ) ) { | |
$found_intro = true; | |
} | |
} | |
} | |
return $servers; | |
} | |
function get_nonuk_nameservers( $whois_out ) { | |
global $domain_re; | |
$servers = []; | |
foreach( $whois_out as $line ) { | |
if (preg_match( '/Name Server:\s+(' . $domain_re . ')/', $line, $matches ) ) { | |
$servers[] = $matches[1]; | |
} | |
} | |
return $servers; | |
} | |
function get_mxs( $mx_out ) { | |
global $domain_re; | |
$mxs = []; | |
foreach( $mx_out as $line ) { | |
if (preg_match( '/IN\s+MX\s+(\d+\s+' . $domain_re . ')/', $line, $matches ) ) { | |
$mxs[] = $matches[1]; | |
} | |
} | |
return $mxs; | |
} | |
function get_hostname( $host_out ) { | |
global $domain_re; | |
$hostname = 'unknown'; | |
foreach( $host_out as $line ) { | |
if (preg_match( '/PTR\s+(' . $domain_re . ')/', $line, $matches ) ) { | |
$hostname = $matches[1]; | |
} | |
} | |
return $hostname; | |
} | |
function output_registrar( $registrar_lines ) { | |
echo "\nRegistrar: \n"; | |
echo " " . implode("\n ", $registrar_lines); | |
echo "\n\n"; | |
} | |
function output_nameservers( $nameservers ) { | |
echo "Nameservers: \n"; | |
echo " " . implode("\n ", $nameservers); | |
echo "\n\n"; | |
} | |
function output_dns( $dns ) { | |
echo "DNS lookup: \n"; | |
echo " $dns\n\n"; | |
} | |
function output_hostname( $hostname, $dns ) { | |
echo "Hostname (of $dns): \n"; | |
echo " $hostname\n\n"; | |
} | |
function output_mxs( $mxs ) { | |
echo "MX records: \n"; | |
echo " " . implode("\n ", $mxs); | |
echo "\n\n"; | |
} | |
$domain = $argv[1]; | |
$whois = exec('whois ' . $domain, $whois_out); | |
$dns = exec('dig +short ' . $domain); | |
$host = exec('dig -x ' . $dns, $host_out); | |
$mx = exec('dig ' . $domain . ' mx', $mx_out); | |
$registrar_lines = get_registrar($whois_out, $domain); | |
$nameservers = get_nameservers($whois_out, $domain); | |
$hostname = get_hostname( $host_out ); | |
$mxs = get_mxs( $mx_out ); | |
output_registrar( $registrar_lines ); | |
output_nameservers( $nameservers ); | |
output_dns( $dns ); | |
output_hostname( $hostname, $dns ); | |
output_mxs( $mxs ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment