PHP: How to Reverse DNS Lookup


Reverse DNS Lookup tools

The Reverse DNS lookup tools are the same as the regular DNS tools. For Windows OS, the easiest way to check the Reverse DNS records of a domain is the “nslookup” command.

Reverse DNS lookup in Windows

Method 1:

nslookup 95.234.212.187

result:

Name: host187-212-dynamic.234-95-r.retail.telecomitalia.it
Address: 95.234.212.187

Note: if you use DNS server like figure below

you take the result:

Server: google-public-dns-a.google.com
Address: 8.8.8.8

Name: host187-212-dynamic.234-95-r.retail.telecomitalia.it
Address: 95.234.212.187

Test code PHP: we use exec function:

exec('nslookup 95.234.212.187',$result);

var_dump($result);

Result:

array(6) {
[0]=>
string(39) “Server: google-public-dns-a.google.com”
[1]=>
string(17) “Address: 8.8.8.8”
[2]=>
string(0) “”
[3]=>
string(61) “Name: host187-212-dynamic.234-95-r.retail.telecomitalia.it”
[4]=>
string(24) “Address: 95.234.212.187”
[5]=>
string(0) “”
}

To check the reverse DNS:

foreach ($result as $line)
{
    if (preg_match("/^Name:(.*?)$/", $line, $match))
    {
        echo 'Reserve DNS: ' . trim($match[1]);
        break;
    }
}

result we take:

Reserve DNS: host187-212-dynamic.234-95-r.retail.telecomitalia.it

Method 2:: You can use ping command:

ping -a 98.139.183.24

result:

Pinging ir2.fp.vip.bf1.yahoo.com [98.139.183.24] with 32 bytes of data:
Reply from 98.139.183.24: bytes=32 time=806ms TTL=37
Reply from 98.139.183.24: bytes=32 time=766ms TTL=37
Reply from 98.139.183.24: bytes=32 time=735ms TTL=37
Reply from 98.139.183.24: bytes=32 time=801ms TTL=37

Ping statistics for 98.139.183.24:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 735ms, Maximum = 806ms, Average = 777ms

Reverse DNS lookup in Linux

Method 1:
We use “host” command to check the reverse DNS:

exec('host 95.234.212.187',$result);

var_dump($result);

Result:

array(1) {
[0]=>
string(101) “187.212.234.95.in-addr.arpa domain name pointer host187-212-dynamic.234-95-r.retail.telecomitalia.it.”
}

To check the reverse DNS:

echo 'Reverse DNS: ' . trim(preg_replace("/^.*?domain name pointer(.*?)$/", "$1", $result[0]), ". ");

result we take:

Reserve DNS: host187-212-dynamic.234-95-r.retail.telecomitalia.it

Method 2:
Use this command:

host 220.220.220.220 | grep -o '[^ ]\+$' | sed s/\.$//

Result:

i220-220-220-220.s41.a013.ap.plala.or.jp

Method 3: use dig command:

dig +short -x "220.220.220.220"  | sed s/\.$//

Leave a Reply