我打了另一个“墙”...我有以下脚本,它似乎在Windows上运行正常,但是当我将它移动到Linux时,它似乎失去了功能。
注意:我添加了打印行语句以进行故障排除。
以下脚本在print lines
时输出hostsFile的内容,然后在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()
感谢任何想法/帮助。
非常感谢提前。
此致
MHibbin
编辑:感谢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()
答案 0 :(得分: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
答案 1 :(得分:0)
问题是你在for
循环后只调用一次通信。在脚本中的那一点,变量ping
引用了循环中对子进程的最后一次调用。
您可以使用空的“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
答案 2 :(得分: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 - 我认为这是你想要的吗?