From mediawiki.org
MediaWiki extensions manual
OOjs UI icon advanced-invert.svg
Release status: unmaintained
Implementation Parser extension
Description Parser and tag functions to perform DNS lookups on hostnames or IP addresses
Author(s) (Jbrandt~mediawikiwikitalk)
Latest version 0.2
License GPL
Download Extension:DNSlookup#Code

Check usage and version matrix.

What can this extension do?[edit]

This extension adds a very simple DNS lookup using the PHP gethostbyname or gethostbyaddr functions depending on if it is called with a hostname or an IP address. It can be used either through a parser function or through a tag.


{{ #dnslookup: www.example.com }}
{{ #dnslookup: }}
<dns lookup='www.example.com' \>
<dns lookup='' \>

It will print the IP address or the reverse lookup of the IP address given. If the parameter does not resolve, it will print an error.

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/DNSlookup.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.


To install this extension, add the following to LocalSettings.php:




$wgExtensionCredits['parserhook'][] = array(
     'name' => 'DNSlookup',
     'author' =>'Josh Brandt', 
     'url' => 'http://www.mediawiki.org/wiki/Extension:DNSlookup', 
     'description' => 'Adds simple dnslookup functions for either hostnames or IP addresses.',
     'version' => '0.2',
# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'efDNSlookup_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'efDNSlookup_Magic';

function efDNSlookup_Setup() {
        global $wgParser;
        # Set a function hook associating the "example" magic word with our function
        $wgParser->setFunctionHook( 'dnslookup', 'efDNSlookup_Render' );
        $wgParser->setHook( 'dns', 'efDNSlookup_Tag');
        return true;
function efDNSlookup_Magic( &$magicWords, $langCode ) {
        $magicWords['dnslookup'] = array( 0, 'dnslookup' );
        return true;

function efDNSlookup_wrapper( $lookup = '' ) {
        if (($binIp = ip2long($lookup)) === false) { 
          # we were given something that isn't a valid IP address-- try it as a hostname.
          $output = gethostbyname($lookup);
          # gethostbyname returns waht it was given if it doesn't resolve. 
          # so, if the paramter is the same as the return, we complain that
          # it doesn't resolve.
          if ( $lookup == $output ) {
               $output = "<span style=\"color: red\">".
                               "Invalid DNS lookup: ".$lookup."</span>";
        } else { 
          # we were given what looks like a valid IP address, so let's find the reverse.
          $output = gethostbyaddr($lookup);
          # gethostbyaddr will return the address if it doesn't find a reverse for it.
          # since we may be building a URL around this, we go ahead and return
          # the address even if it doesn't resolve. 

        return $output;

function efDNSlookup_Render( &$parser, $param1 = '' ) {
        # Disable caching in case the address is changing.
        return ($efDNSlookupreturn);

function efDNSlookup_Tag ( $text, $args, &$parser ) {
        $attr = array();    
        # if we have specified a lookup attribute, we look that up and return.
        foreach( $args as $name => $value ) {
            if ( $name == 'lookup') 
        # if we haven't, we try the text instead.