如何知道pexpect命令实际运行?

2024-06-07 10:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个命令@“工具——服务器”=commander.company.com“登录用户名”,提示输入“密码”。我使用pexpect运行命令,并期望它,并使用sendline 发送密码。我运行它时没有错误,但是这些命令似乎没有运行。我怎么知道这真的发生了?在

child = pexpect.spawn ('tool --server=commander.company.com login username')
child.expect('Password:')
child.sendline('#password')

Tags: 工具命令服务器comchild密码server错误
2条回答

您可以在此处查看两个可能的内容:

  • p.isalive()

isalive(self) method of pexpect.spawn instance This tests if the child process is running or not. This is non-blocking. If the child was terminated then this will read the exitstatus or signalstatus of the child. This returns True if the child process appears to be running or False if not. It can take literally SECONDS for Solaris to return the right status.

  • p.status

示例:

>>> from pexpect import spawn
>>> p = spawn("ls")
>>> p.isalive()
False
>>> p.status
0

有关详细信息,请参见:pexpect Documetnation

child.isalive()child.status对于确定命令的执行状态很有用,但是,pexpect.spawn也可以传递一个打开的文件,输入和输出将被记录到其中。然后,您可以查看派生的进程是否按预期执行,例如

import sys
child = pexpect.spawn('tool  server=commander.company.com login username', logfile=sys.stdout)
child.expect('Password:')
child.sendline('#password')

这将把所有输入和输出发送到标准输出流。logfile_read和{}也可能有用。在

相关问题 更多 >

    热门问题