0

我碰到了另一面“墙”...我有以下脚本,它似乎在 Windows 上运行良好,但是当我将它移到 Linux 时,它似乎失去了功能。

注意:我添加了打印行语句以进行故障排除。

以下脚本在 I 时输出 hostsFile 的内容,print lines然后在 I 时依次打印每一行print line。但是当它执行 ping 时,它似乎直接跳转到文件中的最后一个主机。我想知道我是否遗漏了一些明显的东西(我还在学习 python 作为新手)。

import sys, os, platform, subprocess

plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir,'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
print lines
if plat == "Windows":
        for line in lines:
                line = line.strip( )
                ping = subprocess.Popen(
                        ["ping", "-n", "1", "-l", "1", "-w", "100", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
        out, error = ping.communicate()
        print out
        print error

elif plat == "Linux":
        for line in lines:
                print line
                line = line.strip()
                ping = subprocess.Popen(
                        ["ping", "-c", "1", "-s", "1", "-l", "1",line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
        out, error = ping.communicate()
        print out
        print error

hostsFile.close()

任何想法/帮助表示赞赏。

提前谢谢了。

问候,

米宾

编辑:感谢 Wooble 的帮助......正确的代码应该是(注意间距):

import sys, os, platform, subprocess

plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir,'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
if plat == "Windows":
        for line in lines:
                line = line.strip( )
                ping = subprocess.Popen(
                        ["ping", "-n", "1", "-l", "1", "-w", "100", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

if plat == "Linux":
        for line in lines:
                line = line.strip()
                ping = subprocess.Popen(
                        ["ping", "-c", "1", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

hostsFile.close()
4

3 回答 3

1

如图所示,此代码不适用于 Windows 或 Linux。您需要在循环内移动 ping.communicate() ,否则它将仅在循环完成后执行。

你的 for 循环应该是这样的:

for line in lines:
    print line
    line = line.strip()
    ping = subprocess.Popen(
                ["ping", "-c", "1", "-s", "1", "-l", "1",line],
                stdout = subprocess.PIPE,
                stderr = subprocess.PIPE
          )
    out, error = ping.communicate()
    print out
于 2012-05-17T11:39:31.630 回答
0

问题是你在for循环之后只调用了一次通信。在脚本中的那个点,变量ping引用了循环中对 subprocess 的最后一次调用。

您可以使用空的“hosts.txt”文件来体验这一点。你甚至不会进入循环,所以你会得到一个 NameError:NameError: name 'ping' is not defined

你应该在循环中调用它!

更新:示例:

for line in lines:
    print line
    line = line.strip()
    ping = subprocess.Popen(
        ["ping", "-c", "1", "-s", "1", "-l", "1",line],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    out, error = ping.communicate()
    print "out:", out
    print "error:", error
于 2012-05-17T11:38:30.690 回答
0

我相信这就是你所追求的:

import sys, os, platform, subprocess

plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir,'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
print lines
if plat == "Windows":
        for line in lines:
                line = line.strip( )
                ping = subprocess.Popen(
                        ["ping", "-n", "1", "-l", "1", "-w", "100", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

elif plat == "Linux":
        for line in lines:
                print line
                line = line.strip()
                ping = subprocess.Popen(
                        ["ping", "-c", "1", "-s", "1", "-l", "1",line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

hostsFile.close()

注意 print 语句增加的缩进,应该在 for 循环中。我刚刚在我的机器上尝试过这个,它正在为 hosts.txt 中的每个结果打印一个 ping - 我假设这就是你想要的?

于 2012-05-17T11:42:27.027 回答