2016-03-14 48 views
0

我想获得主机列表的输出,使用unix命令NSLOOKUP +主机列表,但我得到一个错误,虽然它确实工作,当我只有一个主机。Python,使用基本的unix命令和主机列表

有没有更简单的方法来做我正在做的事情,或者你能帮我修复这个简单的脚本吗?

我的脚本是:

#!/usr/bin/python 

import commands, os, string 

hostname = ['host1', 'host2'] 
response = commands.getoutput("nslookup " + ' '.join(hostname)) 
print response 

错误:

[email protected] ~/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 

回答

1

试试这个:

#!/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

+0

感谢奏效,忘了for循环= { } – aznjonn