无法在pexpect中使用child.before获取数据

时间:2015-03-04 21:54:01

标签: python linux bash scripting pexpect

    ip="99.78.992.02"
    prompt="\$"
    log_file_handle = open(os.path.join(os.getcwd(), "log_file"),"a")
    child = pexpect.spawn("ssh abc@%s" % ip, logfile=log_file_handle) # this works because I have public-private key set up
    child.sendline("ls -l")
    child.expect(prompt)
    print "child.before %s" % child.before
    print "child.after %s" % child.after

我得到的回应是:

    child.before  
    Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.2.0-67-generic-pae i686)

     * Documentation:  https://help.ubuntu.com/

    360 packages can be updated.
    305 updates are security updates.

    New release '14.04.1 LTS' available.
    Run 'do-release-upgrade' to upgrade to it.



    *** /dev/sda4 will be checked for errors at next reboot ***
    *** /dev/sda2 will be checked for errors at next reboot ***

    No mail.
    Last login: Wed Mar  4 13:44:40 2015 from 10.49.92.49
    ls
    abc@mycomputer:~
    child.after $

为什么我没有得到" ls -l"在child.before?有人可以对此发表评论吗?

1 个答案:

答案 0 :(得分:1)

正如J.F. Sebastian在评论中所说,我必须在发出命令之前等待提示。