【问题标题】:Python how to read output from pexpect child?Python如何从pexpect child读取输出?
【发布时间】:2013-07-12 00:07:39
【问题描述】:
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 次打印。我使用了错误的发送命令吗?我尝试了发送、写入、发送,但再也找不到了。

【问题讨论】:

    标签: 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时写的命令?我想我只是修剪直到看到\r\n
    【解决方案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\r")
    child.expect(".*\$ ")
    child.sendline("ls\r")
    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. 预期 EOF 仅在流实际结束时才有效。如果我们要生成一个 shell,我们必须显式添加 exit 作为最后一个命令。
      • 这里没有生成 shell,但这是一个好点。不过,添加了另一件事。
      【解决方案6】:

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

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

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

      到标准输出的示例日志::

      # 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
      

      【讨论】:

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