如何在python中运行nslookup?

时间:2015-11-04 09:22:50

标签: python

首先,我在bash中运行nslookup -q=TXT _netblocks.google.com 8.8.8.8,得到如下结果:

  Server:   8.8.8.8
  Address:  8.8.8.8#53

  Non-authoritative answer:
  _netblocks.google.com text = "v=spf1 ip4:64.18.0.0/20 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:207.126.144.0/20 ip4:209.85.128.0/17 ip4:216.58.192.0/19 ip4:216.239.32.0/19 ~all"

  Authoritative answers can be found from:

现在,我有一个任务是在python中获得相同的结果,并且不能使用os lib来运行bash命令。
我可以使用哪个lib?以及如何使用?

1 个答案:

答案 0 :(得分:0)

虽然您应该已经发布了您已经尝试过的内容(如MCVE页面中所述),但这里有一个使用subprocess的示例:

import subprocess
import shlex

command = "nslookup -q=TXT _netblocks.google.com 8.8.8.8"
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
# if running on Windows, split for ("\r\n")
for line in out.split("\n"):
    # process the line, in our case simply print it
    print(line)