Home | History | Annotate | Download | only in examples
      1 #
      2 # Eric S. Raymond
      3 #
      4 # Greatly modified by Nigel W. Moriarty
      5 # April 2003
      6 #
      7 from pexpect import *
      8 import os, sys
      9 import getpass
     10 import time
     11     
     12 class ssh_session:
     13 
     14     "Session with extra state including the password to be used."
     15 
     16     def __init__(self, user, host, password=None, verbose=0):
     17 
     18         self.user = user
     19         self.host = host
     20         self.verbose = verbose
     21         self.password = password
     22         self.keys = [
     23             'authenticity',
     24             'assword:',
     25             '@@@@@@@@@@@@',
     26             'Command not found.',
     27             EOF,
     28             ]
     29         
     30         self.f = open('ssh.out','w')
     31             
     32     def __repr__(self):
     33 
     34         outl = 'class :'+self.__class__.__name__
     35         for attr in self.__dict__:
     36             if attr == 'password':
     37                 outl += '\n\t'+attr+' : '+'*'*len(self.password)
     38             else:
     39                 outl += '\n\t'+attr+' : '+str(getattr(self, attr))
     40         return outl
     41 
     42     def __exec(self, command):
     43 
     44         "Execute a command on the remote host.    Return the output."
     45         child = spawn(command,
     46                                     #timeout=10,
     47                                     )
     48         if self.verbose:
     49             sys.stderr.write("-> " + command + "\n")
     50         seen = child.expect(self.keys)
     51         self.f.write(str(child.before) + str(child.after)+'\n')
     52         if seen == 0:
     53             child.sendline('yes')
     54             seen = child.expect(self.keys)
     55         if seen == 1:
     56             if not self.password:
     57                 self.password = getpass.getpass('Remote password: ')
     58             child.sendline(self.password)
     59             child.readline()
     60             time.sleep(5)
     61             # Added to allow the background running of remote process
     62             if not child.isalive():
     63                 seen = child.expect(self.keys)
     64         if seen == 2: 
     65             lines = child.readlines()
     66             self.f.write(lines)
     67         if self.verbose:
     68             sys.stderr.write("<- " + child.before + "|\n")
     69         try:
     70             self.f.write(str(child.before) + str(child.after)+'\n')
     71         except:
     72             pass
     73         self.f.close()
     74         return child.before
     75 
     76     def ssh(self, command):
     77 
     78         return self.__exec("ssh -l %s %s \"%s\"" \
     79                                              % (self.user,self.host,command))
     80 
     81     def scp(self, src, dst):
     82 
     83         return self.__exec("scp %s %s@%s:%s" \
     84                                              % (src, session.user, session.host, dst))
     85 
     86     def exists(self, file):
     87 
     88         "Retrieve file permissions of specified remote file."
     89         seen = self.ssh("/bin/ls -ld %s" % file)
     90         if string.find(seen, "No such file") > -1:
     91             return None # File doesn't exist
     92         else:
     93             return seen.split()[0] # Return permission field of listing.
     94 
     95