通过 pexpect.run() 方法处理 运行 突然终止

process run by pexpect.run() method terminates abruptly

当我 运行 下面的代码时,我看到进程甚至在完成之前就终止了。 我通过 运行 手动验证命令 command just works 文件。 cmssso-util 产生大约 1200 lines.Can 的输出,这是一个缓冲区问题。 我通过将 'ls -ltr' 分配给变量命令来验证脚本工作正常。 以下参考文档 link: https://pexpect.readthedocs.io/en/stable/_modules/pexpect/run.html

进口预期
命令="cmsso-util domain-repoint -m execute  --src-emb-admin "+'sourceVcAdmin'+“--replication-partner-fqdn”+'destVc'+“--replication-partner-admin”+'destVcAdmin'+“--dest -域名 " + 'destDomain'

打印("Running 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)
打印(” -  -  -  -  -  -  -  -  -  -  -  -  -  - -”)

断言退出状态为 0 , "Execution Failed"
打印("Successfully Completed Embedded Cross Domain Re-pointing ")

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

    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)value 和 maxread parameters

来完成的