带字符串比较的 While 循环无法正常工作
While loop with string comparison doesn't work correctly
我必须编写用于从 kvm 中删除虚拟机的部分程序。这是用 fabric for python http://www.fabfile.org/ 写的。所以,我需要连接到 kvm 主机,检查 vm 是否 运行ning,将其关闭并删除。
当我尝试放入包含 if 语句的 while 循环时出现问题。它似乎无法正常工作,即使机器状态是 "shut",程序也不会继续,最多只重复 5 次相同的 if 语句。
我尝试了不同类型的字符串比较,例如:
- 如果 "shut" 不在
- == "shut"
- != "shut"
- 我也尝试将 "shut" 作为变量
它将 运行 由 Jenkins 完成,我可以从这段代码中看到的只是 while 循环,当它计数到 5 时再进一步。
running
Domain test-test01.test is being shutdown
running
shut
shut
shut
shut
>>> VM not found on this host, but this is fine 33333
这是一个看似可以但不起作用的代码。 Connection
只是其中一种面料功能,效果很好。它通过 ssh 连接到某些主机。您可以在上面看到我使用以下代码在终端中得到的回复。
removed = False
if vmhosttype == "KVM":
with Connection(vmhostname) as c:
if c.sudo("virsh list --all | grep {0}".format(shortfqdn), warn=True, hide="stdout").failed:
print(">>> VM not found on {0}.".format(vmhostname))
else:
print(">>> VM found on {0}.".format(vmhostname))
if c.sudo("virsh list --all | grep {0} | awk '{{ print }}'".format(shortfqdn)) == "shut":
c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
removed = True
else:
c.sudo("virsh shutdown {0}".format(shortfqdn), hide=hide)
i = 0
while (i < 5):
if c.sudo("virsh list --all | grep {0} | awk '{{ print }}'".format(shortfqdn)) != "shut":
time.sleep(30)
i += 1
else:
c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
removed = True
i = 5
问题出在空的 stdout
中。
我必须编写用于从 kvm 中删除虚拟机的部分程序。这是用 fabric for python http://www.fabfile.org/ 写的。所以,我需要连接到 kvm 主机,检查 vm 是否 运行ning,将其关闭并删除。
当我尝试放入包含 if 语句的 while 循环时出现问题。它似乎无法正常工作,即使机器状态是 "shut",程序也不会继续,最多只重复 5 次相同的 if 语句。
我尝试了不同类型的字符串比较,例如: - 如果 "shut" 不在 - == "shut" - != "shut" - 我也尝试将 "shut" 作为变量
它将 运行 由 Jenkins 完成,我可以从这段代码中看到的只是 while 循环,当它计数到 5 时再进一步。
running
Domain test-test01.test is being shutdown
running
shut
shut
shut
shut
>>> VM not found on this host, but this is fine 33333
这是一个看似可以但不起作用的代码。 Connection
只是其中一种面料功能,效果很好。它通过 ssh 连接到某些主机。您可以在上面看到我使用以下代码在终端中得到的回复。
removed = False
if vmhosttype == "KVM":
with Connection(vmhostname) as c:
if c.sudo("virsh list --all | grep {0}".format(shortfqdn), warn=True, hide="stdout").failed:
print(">>> VM not found on {0}.".format(vmhostname))
else:
print(">>> VM found on {0}.".format(vmhostname))
if c.sudo("virsh list --all | grep {0} | awk '{{ print }}'".format(shortfqdn)) == "shut":
c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
removed = True
else:
c.sudo("virsh shutdown {0}".format(shortfqdn), hide=hide)
i = 0
while (i < 5):
if c.sudo("virsh list --all | grep {0} | awk '{{ print }}'".format(shortfqdn)) != "shut":
time.sleep(30)
i += 1
else:
c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
removed = True
i = 5
问题出在空的 stdout
中。