简体   繁体   English

如何以编程方式编写nslookup?

[英]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: 由于已经发布了Python和Ruby答案,因此下面是一个PHP示例:

$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 . 对于Python,请参见http://small-code.blogspot.com/2008/05/nslookup-in-python.html For much richer functionality, also in Python, see http://www.dnspython.org/ . 有关Python中更丰富的功能的信息,请参见http://www.dnspython.org/

Socket Class in Ruby. Ruby中的套接字类 See this answer . 看到这个答案

For PHP, you can use gethostbyname and gethostbyaddr . 对于PHP,可以使用gethostbynamegethostbyaddr

For Python, import the socket module and again use gethostbyname and gethostbyaddr . 对于Python,请导入套接字模块,然后再次使用gethostbynamegethostbyaddr

$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. 你不能依靠它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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