2

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?

jigfox
  • 18,057
  • 3
  • 60
  • 73
nonopolarity
  • 146,324
  • 131
  • 460
  • 740

5 Answers5

4

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
  • 15,255
  • 6
  • 55
  • 53
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/ .

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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 number gethostbyaddr() -- map an IP number or hostname to DNS info – Andre Miller May 27 '09 at 19:49
1

Socket Class in Ruby. See this answer.

Community
  • 1
  • 1
rnicholson
  • 4,538
  • 1
  • 22
  • 13
1

For PHP, you can use gethostbyname and gethostbyaddr.

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

Roshan
  • 1,096
  • 7
  • 11
0
$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.