【问题标题】:Python how to read output from pexpect child?Python 如何读取 pexpect child 的输出?
【发布时间】:2023-01-03 08:43:31
【问题描述】:
child = pexpect.spawn ('/bin/bash')
child.sendline('ls')
print(child.readline())
print child.before, child.after

我在输出中使用此代码得到的只是

ls

ls 

但是当我的代码是

child = pexpect.spawn('ls')
print(child.readline())
print child.before, child.after

然后它起作用了,但仅适用于前 2 次打印。我使用了错误的发送命令吗?我试过发送,写,sendline,但找不到了。

【问题讨论】:

    标签: python pexpect


    【解决方案1】:

    预计 beforeafter 属性会在 expect 方法之后填充。在这种情况下最常用的是等待提示(这样你就会知道前面的命令已经执行完毕)。因此,在您的情况下,代码可能如下所示:

    child = pexpect.spawn ('/bin/bash')
    child.expect("Your bash prompt here")
    child.sendline('ls')
    #If you are using pxssh you can use this
    #child.prompt()
    child.expect("Your bash prompt here")
    print(child.before)
    

    【讨论】:

    • 有没有办法忽略你在打印child.before时写的命令?我想我只是修剪直到看到
    【解决方案2】:

    尝试以下操作:

    import pexpect
    child = pexpect.spawn('ls')
    print child.read() # not readline
    

    read() 将为您提供 ls 的全部输出。

    【讨论】:

    • 在我的测试中也适用于spawnu,而其他最佳答案则没有。可能需要先验证命令是否已完成,但对于 ls 可能无关紧要。
    【解决方案3】:
    #!/usr/bin/env python
    
    import pexpect
    child = pexpect.spawn("ssh root@172.16.0.120c -p 2222")
    child.logfile = open("/tmp/mylog", "w")
    child.expect(".*assword:")
    child.send("XXXXXXX
    ")
    child.expect(".*$ ")
    child.sendline("ls
    ")
    child.expect(".*$ ")
    

    去打开你的日志文件:- 去终端

    $gedit /tmp/mylog
    

    根据https://pexpect.readthedocs.io/en/stable/api/pexpect.html#spawn-class

    # In Python 3, we'll use the ``encoding`` argument to decode data
    # from the subprocess and handle it as unicode:
     child = pexpect.spawn('some_command', encoding='utf-8')
     child.logfile = sys.stdout
    

    【讨论】:

    【解决方案4】:

    我想你只需要:

    p = pexpect.spawn('ls')
    p.expect(pexpect.EOF)
    print(p.before)
    

    要么

    p = pexpect.spawn('/bin/ls')
    p.expect(pexpect.EOF)
    print(p.before)
    

    要么

    p = pexpect.spawn('/bin/bash -c "ls"')
    p.expect(pexpect.EOF)
    print(p.before)
    

    甚至

    print(pexpect.run('ls'))
    

    【讨论】:

      【解决方案5】:
      import sys
      import pexpect
      child = pexpect.spawn('ls')
      child.logfile = sys.stdout
      child.expect(pexpect.EOF)
      

      the manual entry on the subject.

      【讨论】:

      • 警告:如果child.sendline(...) 是程序的最后一行,则不会(有时间)捕获输出。期待 EOF 有帮助:child.expect(pexpect.EOF)pexpect.readthedocs.io/en/stable/…
      • @VictorSergienko 编辑。谢谢!
      • 更多注意事项: 1. 我会先分配 logfile。 2. Expecting EOF 只有在流实际结束时才有效。如果我们生成一个 shell,我们必须显式添加 exit 作为最后一个命令。
      • 没有在这里生成 shell,但这是一个好点。不过,添加了另一件事。
      【解决方案6】:

      从 class spawn(SpawnBase) 文档字符串中复制,也许 example-2 就是您想要的。

      示例日志输入和输出到文件::

      child = pexpect.spawn('some_command')
      fout = open('mylog.txt','wb')
      child.logfile = fout
      

      示例日志到 stdout::

      # In Python 2:
      child = pexpect.spawn('some_command')
      child.logfile = sys.stdout
      
      # In Python 3, we'll use the ``encoding`` argument to decode data
      # from the subprocess and handle it as unicode:
      child = pexpect.spawn('some_command', encoding='utf-8')
      child.logfile = sys.stdout
      

      【讨论】:

        【解决方案7】:

        您可以使用一个简单的包 tiotrap,其中包含用于管理 TextIO 流的 TextIOTrap 帮助类。

        安装:

        python3 -m pip install tiotrap
        

        例如:

        tio_trap = tiotrap.TextIOTrap(store=True)
        p = pexpect.spawn('ls -la')
        p.logfile = tio_trap
        p.expect(pexpect.EOF)
        print(f"ls output:
        {str(tio_trap)}
        ~")
        

        【讨论】:

          猜你喜欢
          • 2013-07-12
          • 2013-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多