X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=scripts%2Fexamples%2Fdnslookup.php;fp=scripts%2Fexamples%2Fdnslookup.php;h=84ad4bcb72a596071d94642700abce03696c731e;hp=0000000000000000000000000000000000000000;hb=5cd595edf6322d8968be3709dab8ae84012fa79e;hpb=58b5a3c9d422799f1a5d612cc6ea8afb6fdecca5 diff --git a/scripts/examples/dnslookup.php b/scripts/examples/dnslookup.php new file mode 100644 index 0000000..84ad4bc --- /dev/null +++ b/scripts/examples/dnslookup.php @@ -0,0 +1,180 @@ +. + */ + +function time2str($time, $anz = 9) { + $str=""; + if(!$anz) $anz=9; + if($time>=(60*60*24*365) && $anz > 0) { + $anz--; + $years=floor($time/(60*60*24*365)); + $str.=$years."Y"; + $time=$time-((60*60*24*365)*$years); + } + if($time>=(60*60*24*30) && $anz > 0) { + $anz--; + $months=floor($time/(60*60*24*30)); + if($str != "") $str.=" "; + $str.=$months."M"; + $time=$time-((60*60*24*30)*$months); + } + if($time>=(60*60*24) && $anz > 0) { + $anz--; + $days=floor($time/(60*60*24)); + if($str != "") $str.=" "; + $str.=$days."d"; + $time=$time-((60*60*24)*$days); + } + if($time>=(60*60) && $anz > 0) { + $anz--; + $stunden=floor($time/(60*60)); + if($str != "") $str.=" "; + $str.=$stunden."h"; + $time=$time-((60*60)*$stunden); + } + if($time>=(60) && $anz > 0) { + $anz--; + $min=floor($time/(60)); + if($str != "") $str.=" "; + $str.=$min."m"; + $time=$time-((60)*$min); + } + if(($time>1 || $str == "") && $anz > 0){ + $anz--; + if($str != "") $str.=" "; + $str.=$time."s"; + } + return $str; +} + + +$host = $argv[1]; +$show_record = strtoupper($argv[2]); + +$pattern_ipv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/'; +$pattern_ipv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/'; + +$record_order = array("SOA", "NS", "A", "AAAA", "MX"); + +function show_subrecords($host) { + $dns = dns_get_record($host, DNS_ALL); + if(count($dns)) { + usort($dns, "sort_records"); + foreach($dns as $record) { + switch($record['type']) { + case "A": + echo" \002A \002 ".$record['ip'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n"; + break; + case "AAAA": + echo" \002AAAA \002 ".$record['ipv6'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n"; + break; + case "CNAME": + echo" \002CNAME \002 ".$record['target']."\n"; + break; + } + } + } +} + +function sort_records($a, $b) { + global $record_order; + $index_a = array_search($a['type'], $record_order); + if($index_a === FALSE) $index_a = count($record_order); + $index_b = array_search($b['type'], $record_order); + if($index_b === FALSE) $index_b = count($record_order); + $order = $index_a - $index_b; + if($order == 0) { + switch($record['type']) { + case "A": + $suborder = "ip"; + break; + case "AAAA": + $suborder = "ipv6"; + break; + case "TXT": + $suborder = "txt"; + break; + default: + $suborder = "target"; + break; + } + $order = strcmp($a[$suborder], $b[$suborder]); + } + return $order; +} + +if(strlen($host) && preg_match("#^((([a-z0-9-.]*)\.|)([a-z]{2,5})|).?$#i", $host)) { + $dns = dns_get_record($host, DNS_ALL); + echo"DNS Records for \002".$host."\002:"; + if($show_record != "" && $show_record != "*" && $show_record != "ANY") + echo" (".$show_record.")"; + echo"\n"; + if(count($dns)) { + usort($dns, "sort_records"); + foreach($dns as $record) { + if($show_record != "" && $show_record != "*" && $show_record != "ANY" && $show_record != $record['type']) + continue; + switch($record['type']) { + case "A": + echo" \002A \002 ".$record['ip'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n"; + break; + case "AAAA": + echo" \002AAAA \002 ".$record['ipv6'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n"; + break; + case "MX": + echo" \002MX \002 ".$record['target']." (priority: ".$record['pri'].")\n"; + if($show_record == "MX") { + show_subrecords($record['target']); + } + break; + case "NS": + echo" \002NS \002 ".$record['target']."\n"; + if($show_record == "NS") { + show_subrecords($record['target']); + } + break; + case "CNAME": + echo" \002CNAME \002 ".$record['target']."\n"; + break; + case "TXT": + echo" \002TXT \002 ".$record['txt']."\n"; + break; + case "SOA": + if($show_record != "SOA") { + echo" \002SOA \002 (see: dns ".$host." SOA)\n"; + break; + } + echo" \002SOA \002 (Start of Authority):\n"; + echo" name: ".$record['mname']."\n"; + echo" serial: ".$record['serial']."\n"; + echo" refresh: ".$record['refresh']." (".time2str($record['refresh'], 2).")\n"; + echo" retry: ".$record['retry']." (".time2str($record['retry'], 2).")\n"; + echo" expire: ".$record['expire']." (".time2str($record['expire'], 2).")\n"; + echo" TTL: ".$record['minimum-ttl']." (".time2str($record['minimum-ttl'], 2).")\n"; + break; + } + } + } else + echo"No records found.\n"; +} else if(preg_match($pattern_ipv4, $host) || preg_match($pattern_ipv6, $host)) { + $hostname = gethostbyaddr($host); + echo"Reverse Lookup for \002".$host."\002:\n"; + echo " \002PTR \002 ".$hostname."\n"; +} else { + echo"Invalid Hostname or IP-Address.\n"; +} +?>