简体   繁体   中英

how to write nslookup programmatically?

除了在脚本中使用exec进行nslookup之外,还有没有一种简便的方法可以用PHP,Python或Ruby编程地编写它?

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

Socket Class in Ruby. See this answer .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM