tags:

views:

55

answers:

1

I am setting up a dns lookup form using dns_get_record. I set it up to check the A Record and MX Records of the domain that is input. However, I would like it to also display the IP address of the displayed MX Records. Is this possible?

+1  A: 

No, at least not in one step. You'll have to do another dns request for the "target" of the MX record, which is the "real" address of the mail server.

A simple script could look like this

$email = "[email protected]";
list( $tmp, $email ) = explode( "@", $email );  // Gets the domain name

$dns = dns_get_record( $email, DNS_MX );
if( count($dns) <= 0 )
    die( "Error looking up dns information." ); // Return value is an empty array if there aren't any MX records but domain exists

// Looks up the first returned MX (note that there can be more than one)
// Each MX record has a 'pri' value where the lowest value is the record with the highest priority
$mx = dns_get_record( $dns[0]['target'], DNS_A );
if( count($mx) <= 0 )
    die( "Error looking up mail server." );
$mx = $mx[0]['ip'];

A full blown A and MX record displaying script

$domain = "google.com";

$dns = dns_get_record( $domain, DNS_ANY );
foreach( $dns as $d ) {
    // Only print A and MX records
    if( $d['type'] != "A" and $d['type'] != "MX" )
        continue;
    // First print all fields
    echo "--- " . $d['host'] . ": <br />\n";
    foreach( $d as $key => $value ) {
        if( $key != "host" )    // Don't print host twice
            echo " {$key}: {$value} <br />\n";
    }
    // Print type specific fields
    switch( $d['type'] ) {
        case 'A':
            // Display annoying message
            echo "A records always contain an IP address. <br />\n";
            break;
        case 'MX':
            // Resolve IP address of the mail server
            $mx = dns_get_record( $d['target'], DNS_A );
            foreach( $mx as $server ) {
                echo "The MX record for " . $d['host'] . " points to the server " . $d['target'] . " whose IP address is " . $server['ip'] . ". <br />\n";
            }
            break;
    }
}
svens
Could you maybe provide an example of how I would set that up?
Batfan
Added an (untested) example.
svens
Hmmm, been trying to implement this into my script and I'm having issues. This is the base setup that I'm usinghttp://bit.ly/dxxush
Batfan
You'd better post your source. You own me a cookie for the second example script :).
svens
@svens - that works perfectly. thanks so much! One more quick question, if I wanted to add the 'www' cname to the records I am checking ( I'm assuming theres no way to specify what CNAME I'd like to display) how would I add an additional A Record lookup for www + the variable? ie: www.$Domain
Batfan
I will definately post the full source once it's finished :)
Batfan