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
46
55
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
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 Download file from server using sftp and returns file contents as a string
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
100 self.ssh_connect()
101 self._client = self._ssh.open_sftp()
102
103 fstat = self._client.stat(self._path)
104 if length > 0:
105
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
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