VanGans

DNS Lookup

Apr 14th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <form action="dns.php" method="POST">
  2. Hostname: <input type="text" name="host" />
  3. Type: <select name="type">
  4. <option value="a">A</option>
  5. <option value="mx">MX</option>
  6. <option value="ns">NS</option>
  7. <option value="txt">TXT</option> </select>
  8. <input type="submit" name="submit" value="Submit" />
  9. </form>
  10.  
  11. <?
  12.  
  13. if(!empty($_POST['host']) && !empty($_POST['type'])){
  14.  
  15.     // Grab variable from form and define valid types
  16.  
  17.     $host = $_POST['host'];
  18.     $type = strtoupper($_POST['type']);
  19.     $validtypes=array("A","MX","NS","TXT");
  20.  
  21.     // Check that dns type is defined or allowed
  22.  
  23.     if(!defined("DNS_" . $type) or !in_array($type,$validtypes)){
  24.        echo "Invalid DNS Type!";
  25.     }else{
  26.  
  27.        $type = constant("DNS_" . $type);
  28.        $rec = dns_get_record($host, $type);
  29.  
  30.        // Set result types - can be modified by using available elements from $rec array
  31.  
  32.        switch($type){
  33.              case DNS_A:
  34.                     $recvals=array("Hostname" => "host","Type" => "type", "IP" => "ip");
  35.                     break;
  36.              case DNS_MX:
  37.                     $recvals=array("Hostname" => "host","Type" => "type", "Target" => "target", "Priority" => "pri");
  38.                     break;
  39.              case DNS_NS:
  40.                     $recvals=array("Hostname" => "host","Type" => "type", "Target" => "target");
  41.                     break;
  42.              case DNS_TXT:
  43.                     $recvals=array("Hostname" => "host","Type" => "type", "Record" => "txt");
  44.                     break;
  45.         }
  46.  
  47.       // Output results
  48.  
  49.       foreach ($rec as $arr => $num){
  50.              foreach ($recvals as $title => $value){
  51.                     echo $title . " : " . $num[$value] . "\n";
  52.              }
  53.       }
  54.  
  55.     }
  56. } else {
  57.  
  58.      echo "Either hostname or record type is missing";
  59. }
Add Comment
Please, Sign In to add comment