Question

instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?

Was it helpful?

Solution

Socket Class in Ruby. See this answer.

OTHER TIPS

Yes, although the function names might not be what you expect.

Since the Python and Ruby answers are already posted, here is an PHP example:

$ip = gethostbyname('www.example.com');
$hostname = gethostbyaddr('127.0.0.1');

For Python see http://small-code.blogspot.com/2008/05/nslookup-in-python.html . For much richer functionality, also in Python, see http://www.dnspython.org/ .

For PHP, you can use gethostbyname and gethostbyaddr.

For Python, import the socket module and again use gethostbyname and gethostbyaddr.

$ip = gethostbyname('www.example.com');

This will work but please be aware that it will be affected if the user changes their hosts file. You can't rely on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top