A little help with pexpect

Discussion in 'Python' started by Hussein B, Jul 19, 2009.

  1. Hussein B

    Hussein B Guest

    Hey,
    I'm trying to execute a command over a remore server using pexpect
    +++++++++++++++++
    url = 'ssh internalserver'
    res = pexpect.spawn(url)
    print '1'
    res.expect('.*ssword:')
    print '2'
    res.sendline('mypasswd')
    print '3'
    res.sendline('ls -aslh')
    +++++++++++++++++
    What I want to do is to send a couple of commands and get the
    response.
    How to do this?
    Thanks.
     
    Hussein B, Jul 19, 2009
    #1
    1. Advertising

  2. >>>>> Hussein B <> (HB) wrote:

    >HB> Hey,
    >HB> I'm trying to execute a command over a remore server using pexpect
    >HB> +++++++++++++++++
    >HB> url = 'ssh internalserver'
    >HB> res = pexpect.spawn(url)
    >HB> print '1'
    >HB> res.expect('.*ssword:')
    >HB> print '2'
    >HB> res.sendline('mypasswd')
    >HB> print '3'
    >HB> res.sendline('ls -aslh')
    >HB> +++++++++++++++++
    >HB> What I want to do is to send a couple of commands and get the
    >HB> response.
    >HB> How to do this?
    >HB> Thanks.


    You can read the output with res.readline() in a loop or similar. The
    problem is when to stop. You could look in the returned string for the
    prompt and hope the prompt doesn't accidentally occur in the output.

    You can also do res.expect(prompt) and get res.before() Same problem
    with accidentally occurrence of the prompt.

    Another way is to use a timeout, e.g. with read_nonblocking or
    specifying a timeout at the spawn call.

    You can also consider using paramiko instead of pexpect. It gives you
    considerably more control. For each command you van open a `channel' and
    that acts more or less similar to a socket, i.e. you get a decent EOF at
    the end of the command's output. This implies that you have to create a
    new channel for each command, but it stays within the same SSH
    connection (SSH allows subconnections).

    Example:

    >>> import paramiko

    ....Mumbles about deprecated modules...
    >>> hostname='server.example.net'
    >>> port = 22
    >>> t = paramiko.Transport((hostname, port))
    >>> username = 'user'
    >>> password = '********'
    >>> t.connect(username=username, password=password)


    Open a channel for a command

    >>> chan = t.open_session()
    >>> chan.exec_command('ls -l')
    >>> chan.recv(999999)

    'total 0\ndrwxr-xr-x 2 user group 60 Apr 2 2009 Mail\ndrwx------ 2 user group 6 Dec 27 2008 tmp\n'
    >>> chan.recv(999999)

    ''

    That was end of file.
    Open a new channel for a new command

    >>> chan = t.open_session()
    >>> chan.exec_command('cat')
    >>> chan.send('abcdefghijklmn\n')

    15
    >>> chan.recv(99)

    'abcdefghijklmn\n'
    >>>

    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 19, 2009
    #2
    1. Advertising

  3. >>>>> Piet van Oostrum <> (PvO) wrote:
    [snip]
    >PvO> You can also consider using paramiko instead of pexpect.

    [snip]
    >>>>> chan = t.open_session()
    >>>>> chan.exec_command('cat')
    >>>>> chan.send('abcdefghijklmn\n')


    In a real program it is better to use sendall here, as send may decide
    to send only part of its argument (the result will tell how many bytes
    have been sent).
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 19, 2009
    #3
    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. pexpect regex help

    , Feb 21, 2007, in forum: Python
    Replies:
    4
    Views:
    780
    amadain
    Feb 23, 2007
  2. ThaDoctor
    Replies:
    3
    Views:
    403
    Alan Woodland
    Sep 28, 2007
  3. Saqib Ali
    Replies:
    1
    Views:
    180
    Nick Dokos
    Dec 20, 2011
  4. Saqib Ali
    Replies:
    1
    Views:
    375
    Roy Smith
    Dec 26, 2011
  5. Daniel
    Replies:
    1
    Views:
    223
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page