我有一个脚本,该脚本使用2台不同的机器运行测试用例。测试需要在机器1上运行某些命令,然后在机器2上运行命令,然后机器1将数据发送到机器2。目标是能够从任一机器上运行测试,所以我认为我将SSH到机器1和在此处执行命令,然后SSH到计算机2并在此处执行命令...我试图避免使用paramiko,因为我不希望额外的依赖关系,所以我发现这段漂亮的代码可以完成这项工作:
def executeRemoteCommand(host, command):
ssh = subprocess.Popen(["ssh", "%s" % host, command],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
这是我在做什么的一个例子:
executeRemoteCommand(user@machine1, "cd /dcd-netapp1/test/test-priority_v6; ./prerun")
time.sleep(30)
executeRemoteCommand(user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./run")
time.sleep(1800)
executeRemoteCommand(user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./postrun")
executeRemoteCommand(user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./graph_results")
问题是我的prerun
ssh会话没有被杀死。该postrun
脚本负责终止以该prerun
脚本启动的日志记录脚本,但是就像我说的那样,当我查看所有正在运行的进程时,在测试结束后ssh会话会很好地显示ps -ef | grep ssh
有关其他信息,我曾经在executeRemoteCommand
函数中包含以下代码:
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
我将其注释掉,因为prerun
脚本将挂起,等待std输出放入结果。这永远不会发生。我的prerun
脚本确实具有std输出,但是我认为它无法收集,因为它可能是一种守护进程?我对prerun
脚本不是很了解。
删除管道对我的情况有效。现在,我从开始测试的计算机上的远程计算机获得所有输出,一切正常关闭。顺便说一句,我发现shell=False
默认情况下它是假的,因此它是不必要的,%s % host
字符串替换也是如此。这是关于我的确切问题的样子:
def executeRemoteCommand(host, command):
ssh = subprocess.Popen(["ssh", host, command])
由于该函数现在已被超级简化,因此我走了一步,认为如果完全放弃该函数并Popen
直接使用它,则该测试更易于阅读:
subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./prerun"])
time.sleep(5)
subprocess.Popen(["ssh", user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./run"])
time.sleep(1800)
subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./postrun"])
subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./graph_results"])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句