diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-11-23 15:30:16 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-11-23 15:30:16 +0000 |
commit | 005593432390183fe75a2baedf27eec57d4da151 (patch) | |
tree | b89c0bffe4f8e153364ff371c4a29e58b9c012f3 | |
parent | c80d46fc002bbc0cb6a6646be0ee3fa43002ffcd (diff) | |
download | pexpect-005593432390183fe75a2baedf27eec57d4da151.tar.gz |
foo
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@208 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r-- | pexpect/examples/ssh_session.py | 65 |
1 files changed, 57 insertions, 8 deletions
diff --git a/pexpect/examples/ssh_session.py b/pexpect/examples/ssh_session.py index 5e05298..6ff04aa 100644 --- a/pexpect/examples/ssh_session.py +++ b/pexpect/examples/ssh_session.py @@ -1,8 +1,14 @@ # # Eric S. Raymond # +# Greatly modified by Nigel W. Moriarty +# April 2003 +# from pexpect import * - +import os, sys +import getpass +import time + class ssh_session: "Session with extra state including the password to be used." def __init__(self, user, host, password=None, verbose=0): @@ -10,24 +16,66 @@ class ssh_session: self.host = host self.verbose = verbose self.password = password + self.keys = [ + 'authenticity', + 'assword:', + '@@@@@@@@@@@@', + 'Command not found.', + EOF, + ] + + self.f = open('ssh.out','w') + + def __repr__(self): + outl = 'class :'+self.__class__.__name__ + for attr in self.__dict__: + if attr == 'password': + outl += '\n\t'+attr+' : '+'*'*len(self.password) + else: + outl += '\n\t'+attr+' : '+str(getattr(self, attr)) + return outl + def __exec(self, command): - "Execute a command on the remote host. Return the output." - child = spawn(command) + "Execute a command on the remote host. Return the output." + child = spawn(command, + #timeout=10, + ) if self.verbose: sys.stderr.write("-> " + command + "\n") - seen = child.expect(['assword:', EOF]) + seen = child.expect(self.keys) + self.f.write(str(child.before) + str(child.after)+'\n') if seen == 0: + child.sendline('yes') + seen = child.expect(self.keys) + if seen == 1: if not self.password: self.password = getpass.getpass('Remote password: ') child.sendline(self.password) - seen = child.expect(EOF) + child.readline() + time.sleep(5) + # Added to allow the background running of remote process + if not child.isalive(): + seen = child.expect(self.keys) + if seen == 2: + lines = child.readlines() + self.f.write(lines) if self.verbose: - sys.stderr.write("<- " + child.before + "\n") + sys.stderr.write("<- " + child.before + "|\n") + try: + self.f.write(str(child.before) + str(child.after)+'\n') + except: + pass + self.f.close() return child.before + def ssh(self, command): - return self.__exec("ssh -l %s %s \"%s\""%(self.user,self.host,command)) + return self.__exec("ssh -l %s %s \"%s\"" \ + % (self.user,self.host,command)) + def scp(self, src, dst): - return self.__exec("scp %s %s@%s:%s" % (src, session.user, session.host, dst)) + return self.__exec("scp %s %s@%s:%s" \ + % (src, session.user, session.host, dst)) + def exists(self, file): "Retrieve file permissions of specified remote file." seen = self.ssh("/bin/ls -ld %s" % file) @@ -35,3 +83,4 @@ class ssh_session: return None # File doesn't exist else: return seen.split()[0] # Return permission field of listing. + |