我正在尝试使用unix命令nslookup +主机列表来获取主机列表的输出,但是我收到错误,尽管当我只有一个主机时它确实有效。
有没有更简单的方法来做我正在做的事情,还是可以帮我修复这个简单的脚本?
我的脚本是:
#!/usr/bin/python
import commands, os, string
hostname = ['host1', 'host2']
response = commands.getoutput("nslookup " + ' '.join(hostname))
print response
错误:
user@hostname ~/scripts> ./nslookup
^CTraceback (most recent call last):
File "./nslookup", line 6, in <module>
response = commands.getoutput("nslookup " + ' '.join(hostname))
File "/usr/lib64/python2.6/commands.py", line 46, in getoutput
return getstatusoutput(cmd)[1]
File "/usr/lib64/python2.6/commands.py", line 56, in getstatusoutput
text = pipe.read()
KeyboardInterrupt
答案 0 :(得分:1)
试试这个:
#!/usr/bin/python
import commands, os, string
hostnames = ['host1', 'host2']
for hostname in hostnames:
response = commands.getoutput("nslookup " + hostname)
print response