Package tlib :: Package base :: Module SshFileRequestor |
[hide private]
[frames] | no frames]

# Source Code for Module tlib.base.SshFileRequestor

      1 -class SshFileRequestor(object):
      2      """
      3      Helper class for retrieving files from remote server
      4      """
      5      _host = None
      6      _username = None
      7      _password = None
      8      _pkey = None
      9      _keyfile = None
     10
     11      _path = None
     12      _size = None
     13      _start = 0
     14
     15      _ssh = None
     16      _client = None
     17      _keepalive = None
     18
     19 -    def __init__(self, host, username=None, password=None, pkey=None, key_filename=None, path=None, keep_alive=True):
     20          """
     21          Constructor for class
     22
     23              @param host: remote hostname
     24              @param username : login username
     25              @param password : login password
     26              @param pkey : private key used for login
     27              @param key_filename : key file used for login
     28              @param path : file path to be retrieved
     29              @param keep_alive : establish only one connection
     30
     31          """
     32          self._host = host
     33          self._username = username
     34          self._password = password
     35          self._pkey = pkey
     36          self._keyfile = key_filename
     37          self._path = path
     38          self._keepalive = keep_alive
     39          self.ssh_connect()
     40
     41 -    def __del__(self):
     42          """
     43          Destructor for class
     44          """
     45          self.ssh_disconnect()
     46
     47 -    def ssh_connect(self):
     48          """
     49          Establish ssh connection
     50          """
     51          if not self._ssh or not self._keepalive:
     52              self._ssh = paramiko.SSHClient()
     53              self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     54              self._ssh.connect(hostname=self._host, username=self._username, password=self._password, pkey=self._pkey, key_filename=self._keyfile)
     55
     56 -    def ssh_disconnect(self):
     57          """
     58          Destroy ssh connection
     59          """
     60          if self._client:
     61              self._client.close()
     62              self._client = None
     63          if self._ssh:
     64              self._ssh.close()
     65              self._ssh = None
     66
     67 -    def remote_tail(self, remote_host, path=None, length=0):
     68          """
     69          Tail the file from a remote host (current connection used for hop purpose):
     70          Please make sure the new ssh remote login does not require to input credentials
     71
     72          @param remote_host: target server where the file is located
     73          @param path: path to file on the target server
     74          @param length: length used for tailing
     75
     76          @return: a list of last n lines of file
     77          """
     78          if path :
     79              self._path = path
     80          # make sure there's a connection
     81          self.ssh_connect()
     82          std_in, std_out, std_err = self._ssh.exec_command('ssh -t %s tail -%i %s' % (remote_host, length, self._path))
     83          lines = std_out.read().strip().split('\n')
     84          return lines[-length:] if len(lines) > length else lines[1:]
     85
     86
     87 -    def tail(self, path=None, length=0):
     88          """
     89          Tail a file on the server
     90
     91          @param path: path to file on the server
     92          @param length: length used for tailing, tailing from last position (or beginning) if length is not provided
     93
     94          @return: a list of last n lines of file
     95          """
     96          if path:
     97              self._path = path
     98          if not self._client:
     99              # make sure there's a connection
    100              self.ssh_connect()
    101              self._client = self._ssh.open_sftp()
    102
    103          fstat = self._client.stat(self._path)
    104          if length > 0:
    105              #we assume that each line may contains MAX_CHARS_IN_ONE_LINE characters at max
    106              self._start = max(0,  fstat.st_size - (MAX_CHARS_IN_ONE_LINE * length))
    107          with self._client.open(self._path, 'r') as f:
    108              # check if we have the file size
    109              f.seek(self._start)
    110              lines = f.read().strip().split('\n')
    111
    112          self._start = fstat.st_size
    113
    114          return lines[-length:] if len(lines) > length else lines[1:]
    115