Pexpect echoes twice for each read

Discussion in 'Python' started by Sriram Rajan, Sep 10, 2008.

  1. Sriram Rajan

    Sriram Rajan Guest

    For some reason, Using pexpect causes my output to echo twice when I
    connect from my MAC Darwin (10.4) to Linux (CentOS release 5 ):

    The program:
    ---------------------
    #!/usr/bin/python
    # Automatic scp to remote host
    # Input 1 : filename
    # Input 2 : destination folder
    # Input 3 : hostname

    import pexpect
    import sys,re

    ssh_cmd = "ssh " + sys.argv[3]
    ssh_handle = pexpect.spawn (ssh_cmd)
    ssh_handle.logfile = sys.stdout
    PROMPT = "\$\ $"

    try:
    ssh_handle.expect(PROMPT)

    ssh_handle.sendline ("scp "+ sys.argv[1] +" root@192.168.1.254:" +
    sys.argv[2] )

    ssh_handle.expect("password:")

    ssh_handle.sendline(" ")

    ssh_handle.expect(PROMPT)

    except pexpect.TIMEOUT:
    ssh_handle.logfile.write("\n Pexpect timeout !!\n")
    sys.exit(1)
    except KeyboardInterrupt:
    ssh_handle.logfile.write("\n User interrupt!\n")
    sys.exit(2)
    ssh_handle.close()

    Output:
    -----------
    $ python pexpect_test.py replace_line_break.sh /tmp/ 10.5.254.18
    Last login: Tue Sep 9 15:45:05 2008 from sriram-macbook.dhcp.2wire.com
    $ scp replace_line_break.sh root@192.168.1.254:/tmp/
    scp replace_line_break.sh root@192.168.1.254:/tmp/
    root@192.168.1.254's password:

    replace_line_break.sh 100% 296 0.3KB/s 00:00
    $ $
     
    Sriram Rajan, Sep 10, 2008
    #1
    1. Advertising

  2. Sriram Rajan

    Guest

    On Sep 9, 4:01 pm, "Sriram Rajan" <> wrote:
    > For some reason, Using pexpect causes my output to echo twice when I
    > connect from my MAC Darwin (10.4) to Linux (CentOS release 5 ):
    >
    > The program:
    > ---------------------
    > #!/usr/bin/python
    > # Automatic scp to remote host
    > # Input 1 : filename
    > # Input 2 : destination folder
    > # Input 3 : hostname
    >
    > import pexpect
    > import sys,re
    >
    > ssh_cmd = "ssh " + sys.argv[3]
    > ssh_handle = pexpect.spawn (ssh_cmd)
    > ssh_handle.logfile = sys.stdout
    > PROMPT = "\$\ $"
    >
    > try:
    > ssh_handle.expect(PROMPT)
    >
    > ssh_handle.sendline ("scp "+ sys.argv[1] +" r...@192.168.1.254:" +
    > sys.argv[2] )
    >
    > ssh_handle.expect("password:")
    >
    > ssh_handle.sendline(" ")
    >
    > ssh_handle.expect(PROMPT)
    >
    > except pexpect.TIMEOUT:
    > ssh_handle.logfile.write("\n Pexpect timeout !!\n")
    > sys.exit(1)
    > except KeyboardInterrupt:
    > ssh_handle.logfile.write("\n User interrupt!\n")
    > sys.exit(2)
    > ssh_handle.close()
    >
    > Output:
    > -----------
    > $ python pexpect_test.py replace_line_break.sh /tmp/ 10.5.254.18
    > Last login: Tue Sep 9 15:45:05 2008 from sriram-macbook.dhcp.2wire.com
    > $ scp replace_line_break.sh r...@192.168.1.254:/tmp/
    > scp replace_line_break.sh r...@192.168.1.254:/tmp/
    > r...@192.168.1.254's password:
    >
    > replace_line_break.sh 100% 296 0.3KB/s 00:00
    > $ $


    Hi,

    I had this issue as well. To correct it I upgraded to pexpect 2.3 from
    2.1 and changed the following:

    OLD: ssh_handle.logfile = sys.stdout
    NEW: ssh_handle.logfile_read = sys.stdout

    This eliminated the double display of characters as only the
    characters being sent back from the device are displayed.

    Let me know if this works for you.

    - Darrel
     
    , Sep 20, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    421
    Eliyahu Goldin
    Aug 3, 2005
  2. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    410
    Kiuhnm
    Apr 1, 2006
  3. dan
    Replies:
    0
    Views:
    299
  4. Saqib Ali
    Replies:
    1
    Views:
    368
    Roy Smith
    Dec 26, 2011
  5. Jim Cain
    Replies:
    0
    Views:
    101
    Jim Cain
    Jul 18, 2003
Loading...

Share This Page