由pexpect.run()方法运行的进程突然终止

时间:2019-02-05 04:08:40

标签: python jenkins pexpect

当我运行以下代码时,我看到该过程甚至在完成之前就终止了。 我通过手动运行命令来验证命令,该命令仅适用于文件。 cmssso-util产生的输出约为1200行,这可能是缓冲区问题吗? 我通过将'ls -ltr'分配给变量命令来验证脚本工作正常。 来自以下链接的参考文档: https://pexpect.readthedocs.io/en/stable/_modules/pexpect/run.html

  • 我尝试使用'bash -c'作为前缀命令,但无法解决此问题。
  • 我试图找出pexpect如何确定终止进程,但仍然无法获得任何清晰的文档。 请帮助我。
import pexpect
command = "cmsso-util domain-repoint -m execute  --src-emb-admin " + 'sourceVcAdmin' + " --replication-partner-fqdn " + 'destVc' + " --replication-partner-admin " + 'destVcAdmin' + " --dest-domain-name " +    'destDomain'

print("Running command  : " + command) 

(command_output, exitstatus) =   pexpect.run(command ,withexitstatus=1, events={'Enter Source embedded vCenter Server Admin Password :' : '\r\n','Enter Replication partner Platform Services Controller Admin Password :' : '\r\n','All Repoint configuration settings are correct; proceed?(.*)' :  'Y\r\n'})

print("----Command output------------")
print(command_output)
print("-----------------------------")

assert exitstatus is 0 , "Execution Failed"
print("Successfully Completed Embedded Cross Domain Re-pointing ")

1 个答案:

答案 0 :(得分:0)

我可以使用以下代码解决此问题:

    import pexpect
try :
        command = "cmsso-util domain-repoint -m execute  --src-emb-admin " + 'sourceVcAdmin' + " --replication-partner-fqdn " + 'destVc' + " --replication-partner-admin " + 'destVcAdmin' + " --dest-domain-name " +    'destDomain'
        print("Running command  : " + command) 
        child = pexpect.spawn(command, timeout=3000,maxread=12000)
        child.expect (['Enter Source embedded vCenter Server Admin Password :'],timeout=40000)
        child.sendline(<password>)
        child.expect (['Enter Replication partner Platform Services Controller Admin Password :'],timeout=40000)
        child.sendline(<password>)
        child.expect (['All Repoint configuration settings are correct; proceed?(.*)'],timeout=40000)
        child.sendline('Y')
        child.expect(pexpect.EOF)
        print(child.before)   
        assert(child.status == 0 , "Operation Failed!", "Successfully Completed Embedded Cross Domain Re-pointing")
except:
    print("Exception was thrown")
    print("debug information:")
    print(str(child))
    child.close()
    exit(1)

这是通过增加默认的child = pexpect.spawn(command,timeout = 600,maxread = 8000)值和 maxread 参数

完成的