Pexpect: SSH, Command and Password

Discussion in 'Python' started by Gil_H, Apr 4, 2007.

  1. Gil_H

    Gil_H Guest

    Hi,

    I'm trying to run a script over unix on a remote machine.
    In order to automate it, the procedure requests the following:
    1. Using SSH connection.
    2. Operating a command on the remote machine.
    3. Expecting password or (yes/no) request and authorize it.

    I get an error (I thing that it occures at the last part (3) of the
    password request).
    I don't have any errors when trying to operate the script without the
    last password.
    Here is the script:

    import pexpect
    import sys
    import re
    import os
    import getopt

    PROMPT = "\$|\%|\>"


    class SSH:
    def __init__(self, user, password, host):
    self.child = pexpect.spawn("ssh %s@%s"%(user, host))
    i = self.child.expect(['assword:', r'yes/no'],
    timeout=120)
    if i==0:
    self.child.sendline(password)
    elif i==1:
    self.child.sendline("yes")
    self.child.expect("assword:", timeout=120)
    self.child.sendline(password)
    self.child.expect(PROMPT)

    def command(self, command, password):
    """send a command and return the response"""
    self.child.expect(PROMPT)
    self.child.sendline(command)
    j = self.child.expect(['assword:', r'yes/no'],
    timeout=120)
    if j==0:
    self.child.sendline(password)
    elif j==1:
    self.child.sendline("yes")
    self.child.expect("assword:", timeout=120)
    self.child.sendline(password)
    self.child.expect(PROMPT)

    # response = self.child.before
    # return response

    def close(self):
    """close the connection"""
    self.child.close()


    if __name__=="__main__":

    ssh = SSH(sys.argv[1], sys.argv[2], sys.argv[3])
    command=sys.argv[4]
    print command
    print "Password: " + sys.argv[2]
    responce=ssh.command(command, sys.argv[2])
    ssh.close()
    print responce


    I'm waiting for your ideas.
    Thanks,
    Gil H.
     
    Gil_H, Apr 4, 2007
    #1
    1. Advertising

  2. >>>>> "Gil" == Gil H <> writes:

    > class SSH:
    > def __init__(self, user, password, host):
    > self.child = pexpect.spawn("ssh %s@%s"%(user, host))


    Try adding the following line here

    self.child.logfile = sys.stdout

    That should give you some clue.

    Ganesan


    --
    Ganesan Rajagopal
     
    Ganesan Rajagopal, Apr 4, 2007
    #2
    1. Advertising

  3. Gil_H

    Gil_H Guest

    On 4 Apr, 15:14, Ganesan Rajagopal <> wrote:
    > >>>>> "Gil" == Gil H <> writes:

    > > classSSH:
    > > def __init__(self, user, password, host):
    > > self.child =pexpect.spawn("ssh%s@%s"%(user, host))

    >
    > Try adding the following line here
    >
    > self.child.logfile = sys.stdout
    >
    > That should give you some clue.
    >
    > Ganesan
    >
    > --
    > Ganesan Rajagopal


    Thanks,

    That's all i needed!
     
    Gil_H, Apr 4, 2007
    #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. crybaby

    pexpect ssh login and ls | grep

    crybaby, Dec 31, 2007, in forum: Python
    Replies:
    6
    Views:
    1,195
    crybaby
    Jan 1, 2008
  2. Sean DiZazzo

    Re: SSH using PEXPECT

    Sean DiZazzo, Sep 11, 2008, in forum: Python
    Replies:
    1
    Views:
    546
    nntpman68
    Sep 13, 2008
  3. AAaron123
    Replies:
    2
    Views:
    2,367
    AAaron123
    Jan 16, 2009
  4. Saqib Ali
    Replies:
    1
    Views:
    381
    Roy Smith
    Dec 26, 2011
  5. Replies:
    0
    Views:
    134
Loading...

Share This Page