views:

916

answers:

4

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?

+1  A: 

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/ .

Alex Martelli
The documentation for the Python socket library http://pydoc.org/1.6/socket.html is also very useful. Have a look at:gethostbyname() -- map a hostname to its IP numbergethostbyaddr() -- map an IP number or hostname to DNS info
Andre Miller
+1  A: 

Socket Class in Ruby. See this answer.

rnicholson
+1  A: 

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');
Andre Miller
+1  A: 

For PHP, you can use gethostbyname and gethostbyaddr.

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

Roshan