tags:

views:

1246

answers:

4
+1  Q: 

DNS Lookup in PHP

I have a Windows server that is intermittently losing the ability to lookup DNS information. I'm trying to get to the root cause of the problem but in the mean time I'd like to be able to monitor whether the server can perform lookups.

Basically, it should attempt to lookup some common hostnames and the display 'Success' if the lookups are successful.

The site runs PHP so I'd prefer that the monitor script be in PHP but if someone knows how to do this in ASP / .Net that would work as well.

+4  A: 

http://www.php.net/manual/en/function.dns-get-record.php is the function in php it sounds like you are after.

Rodney Amato
Thanks. That function wasn't available in Windows the last time I tried this but it was made available in PHP 5.3+. I should have checked it again first.
Dave Forgac
A: 

but there is a little limitation of that function is Changelog: v. PHP 5.3.0 - This function is now available on Windows platforms.

if you dont want to update php on IIS. there is another alternative that executing dig for windows binary. here is dig for windows. you may also need that for your own not for any program. it's beyond of old-not-enough nslookup command.

risyasin
In my case this works because the server is running 5.3 but I was originally asking the question because I believed that the function wasn't available on Windows.
Dave Forgac
+1  A: 

On windows PHP DNS functions are not available natively prior to PHP 5.3. You will need the Pear Net_DNS Class. http://pear.php.net/package/Net_DNS

Example usage:

require_once 'Net/DNS.php';

$resolver = new Net_DNS_Resolver();
$resolver->debug = $this->debug;
// nameservers to query
$resolver->nameservers = array('192.168.0.1');
$resp = $resolver->query($domain, 'A');

source: http://code.google.com/p/php-smtp-email-validation/source/browse/trunk/smtp_validateEmail.class.php#232

bucabay
A: 

"Ping " always performs a DNS lookup (both forward and reverse) before pinging the hostname in question. Writing a shell script to use ping (or dig) to see if ping is acting wonky is left as an exercise to the reader.

Another option is to use a caching DNS server on the local machine that caches replies from the upstream DNS server, and sends data from the cache when upstream is down. My own Deadwood is a tiny 32k Windows or UNIX binary that can do this (64k if you want full DNS recursion)

samiam