Python,使用基本的 unix 命令和主机列表

Python, using basic unix command and list of hosts

我正在尝试使用 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

试试这个:

#!/usr/bin/python

import commands, os, string

hostnames = ['host1', 'host2']

for hostname in hostnames:
    response = commands.getoutput("nslookup " + hostname)
    print response

(http://linux.die.net/man/1/nslookup)