【问题标题】:Python, using basic unix command and list of hostsPython,使用基本的 unix 命令和主机列表
【发布时间】:2016-03-14 21:45:17
【问题描述】:

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

【问题讨论】:

    标签: linux python-2.7 nslookup


    【解决方案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)

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2013-09-23
      • 2012-02-03
      • 2023-03-26
      相关资源
      最近更新 更多