Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pexpect: SSH, Command and Password

Reply
Thread Tools

Pexpect: SSH, Command and Password

 
 
Gil_H
Guest
Posts: n/a
 
      04-04-2007
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.

 
Reply With Quote
 
 
 
 
Ganesan Rajagopal
Guest
Posts: n/a
 
      04-04-2007
>>>>> "Gil" == Gil H <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Gil_H
Guest
Posts: n/a
 
      04-04-2007
On 4 Apr, 15:14, Ganesan Rajagopal <(E-Mail Removed)> wrote:
> >>>>> "Gil" == Gil H <(E-Mail Removed)> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lost Windows login Password? 6 Ways to Recover and Recover windows Password jamesstevn General Computer Support 0 03-10-2010 03:18 AM
Change a users password without knowing the old password nor the answer to the password question AAaron123 ASP .Net 1 01-16-2009 02:56 PM
Changing a users password without knowing the old password nor the answer to the password question AAaron123 ASP .Net 2 01-16-2009 02:08 PM
NCQ (Native Command Queuing) and TCQ (Tagged Command Queuing) Explained Silverstrand Front Page News 0 04-17-2006 05:49 PM
I saved password into the database with "binary" data type from GetByte method. How can I to compare the password that between database and transfered from the page? Benny Ng ASP .Net 3 01-25-2006 06:49 AM



Advertisments