-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy.txt
More file actions
343 lines (296 loc) · 12.2 KB
/
study.txt
File metadata and controls
343 lines (296 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import time
import paramiko
import socket
import glob
import os
class SSHConnection(AbstractBaseConnection):
conn_protocol = 'SSH'
def __init__(self, host, port, timeout):
port = int(port) if port else 22
paramiko.util.log_to_file('./paramiko.log')
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
_log = paramiko.util.get_logger('paramiko')
for handler in _log.handlers:
handler.close()
_log.removeHandler(handler)
self.host = host
self.port = port
self.channel = None
self._prompt = None
self._match_prompt = ''
self.term = 'linux'
self.width = 1024
self.height = 512
self.set_timeout(timeout)
def _login(self, username, password):
paramiko.util.log_to_file('./paramiko.log')
self.username = username
self.password = password
try:
self.client.connect(self.host, self.port, username, password)
except paramiko.AuthenticationException:
raise RuntimeError('%s Authentication Failure' % self.host)
finally:
_log = paramiko.util.get_logger('paramiko')
for handler in _log.handlers:
handler.close()
_log.removeHandler(handler)
def _close(self):
try:
if self.channel:
self.channel.send('exit\n')
except socket.error:
pass
self.channel = None
self.client.close()
print
'*INFO* Disconnect from %s' % self.host
def write(self, text):
if self.channel is None:
self.channel = self.client.invoke_shell(term=self.term, width=self.width, height=self.height)
self.channel.set_combine_stderr(True)
try:
print
text
self.channel.sendall(text + self._newline)
except socket.error:
raise EOFError
def read(self):
data = ''
if self.channel is None:
self.channel = self.client.invoke_shell(term=self.term, width=self.width, height=self.height)
self.channel.set_combine_stderr(True)
while self.channel.recv_ready():
_raw = self.channel.recv(1000)
if _raw == '':
time.sleep(0.001)
continue
data += _raw
return data
def _reconnect(self):
self.channel = None
self.client.close()
del (self.client)
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(self.host, self.port, self.username, self.password)
self.read_until_command_prompt()
def get_file(self, remotepath, localpath, callback=None):
self.sftp = paramiko.SFTPClient.from_transport(self.client._transport)
return self.sftp.get(remotepath, localpath, callback)
def put_file(self, source, destination, callback=None, confirm=True):
mode = 00777 # stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
filelist = glob.glob(source)
dest_dir, dest_file = os.path.split(destination)
if len(filelist) > 1 and dest_file != '':
raise ValueError, 'Can\'t copy multiple source files to one destination file!'
print
"*INFO* Copying %s to %s" % (filelist, dest_dir)
if dest_dir.count('\\') > 0:
dirnames = dest_dir.replace('\\', '/').split('/')
else:
dirnames = dest_dir.split('/')
sftp_client = self.client.open_sftp()
for dirname in dirnames:
if dirname == '' or dirname.count(':') > 0:
sftp_client.chdir('/')
continue
else:
try:
sftp_client.chdir(dirname)
except IOError:
sftp_client.mkdir(dirname)
sftp_client.chdir(dirname)
cwd = sftp_client.getcwd()
for singlefile in filelist:
if len(filelist) == 1 and dest_file != '' and os.path.isfile(singlefile):
sftp_client.put(singlefile, cwd + '/' + dest_file)
sftp_client.chmod(cwd + '/' + dest_file, mode)
print
"*INFO* Copied file from '%s'\n to '%s'" % (singlefile, cwd + '/' + dest_file)
elif os.path.isfile(singlefile):
sftp_client.put(singlefile, cwd + '/' + os.path.split(singlefile)[1])
sftp_client.chmod(cwd + '/' + os.path.split(singlefile)[1], mode)
print
"*INFO* Copied file from '%s'\n to '%s'" % (singlefile, cwd + '/' + os.path.split(singlefile)[1])
class AbstractBaseConnection(object):
_answer_prompt = '\[[Yy]/[Nn]\]: | yes or no: |n]: |[Pp]assword:\s+$|(yes/no)|\[[Yy]/n\]:|\[y/n\]|Y/N."|~$'
_shell_prompt = '\[\S+@.*\]\s*[$#]\s*$|bash\-.*[$#]\s*$|lim-bash[$#]\s*$|\w+\-\d+:\~?.*\$\s*$|\w+@[\w-]+:\~?.*[$#]\s*'
_scli_prompt = '.*@.*\s+\[.*\]\s+>\s*$'
_custom_prompt = ''
_encoding = 'utf8'
_newline = '\n'
_timeout = 10
_console_type = ''
def __init__(self, host='', port='', prompt='', timeout=''):
pass
def _login(self, username, password):
raise_xxxError('xxxKeywordSyntaxError', 'The Function has been not implemented yet')
def _close(self):
raise_XXXError('xxxKeywordSyntaxError', 'The Function has been not implemented yet')
def write(self, command):
raise_XXXError('xxxKeywordSyntaxError', 'The Function has been not implemented yet')
def read(self):
raise_XXXError('xxxKeywordSyntaxError', 'The Function has been not implemented yet')
def _reconnect(self):
raise NotImplementedError
def _encode(self, text):
if isinstance(text, str):
return text
if not isinstance(text, basestring):
text = unicode(text)
return text.encode(self._encoding)
def _decode(self, text):
text = re.sub('\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K|C]?', '', text)
try:
return text.decode(self._encoding, 'ignore')
except UnicodeEncodeError:
return text
def read_until(self, expected):
data = ''
max_time = time.time() + self._timeout
while time.time() < max_time:
_raw = self._decode(self.read())
if _raw != '':
data += _raw
continue
if expected in data:
print '*INFO*', data
return data
print '*INFO*', data
raise_XXXError('xxxCommandExecuteError',
'Read the output with expected string(%s) timeout!' % expected)
def read_until_command_prompt(self):
data = ''
_shell_prompt_ptn = re.compile(self._shell_prompt)
_scli_prompt_ptn = re.compile(self._scli_prompt)
_answer_prompt_ptn = re.compile(self._answer_prompt)
console_ptn_type_map = {_scli_prompt_ptn: 'scli',
_shell_prompt_ptn: 'shell',
_answer_prompt_ptn: ''}
if self._custom_prompt:
_custom_prompt_ptn = re.compile(self._custom_prompt)
console_ptn_type_map[_custom_prompt_ptn] = ''
max_time = self._timeout + time.time()
while time.time() < max_time:
_raw = self._decode(self.read())
if _raw == '':
continue
if _raw.endswith('--More--'):
data = data.rstrip() + '\n' + _raw.replace('--More--', '').strip()
self.write(' ')
continue
data += _raw
search_data = data[-1024:]
for ptn in console_ptn_type_map:
if ptn.search(search_data):
if console_ptn_type_map[ptn]:
self._console_type = console_ptn_type_map[ptn]
print '*INFO*', data
return data
print '*INFO*', data
raise AssertionError("No match found for prompt" + self._shell_prompt + '|' + self._scli_prompt + '|' + self._answer_prompt + '|' + self._custom_prompt)
def connect_to_hardware(self, host, port, username, password, timeout):
port = int(port)
self._timeout = timestr_to_secs(timeout)
self._login(username, password)
return self.read_until_command_prompt()
def disconnect_from_hardware(self):
self._close()
def _remove_prompt(self, output):
prompt_pool = [self._answer_prompt, self._scli_prompt, self._shell_prompt, self._custom_prompt]
return re.sub('|'.join(filter(lambda item: item, prompt_pool)), '', output)
def execute_command(self, command, *answers):
print '*TRACE* entering execute_command'
prompt_pool = [self._scli_prompt, self._shell_prompt, self._custom_prompt]
end_matcher = re.compile('|'.join(filter(lambda item: item, prompt_pool)))
print '*INFO* Execute command:', command
self.write(command)
try:
result = ""
index = 0
max_time = self._timeout + time.time()
answer_len = len(answers)
while time.time() < max_time:
#result += self.read_until_command_prompt()
tmp = self.read_until_command_prompt()
if tmp.strip() == command.strip() or not tmp.strip():
continue
result += tmp
if end_matcher.search(result):
if command[:5] not in result:
continue
tmp_str = self._decode(result).replace(command, '',1)
return self._remove_prompt(tmp_str)
if index == answer_len:
self.write('n')
continue
self.write(answers[index])
index += 1
except EOFError:
raise
except:
print result
print '*WARN* execute_command:\n', traceback.format_exc()
out = self.read()
if not out:
try:
self.write(chr(3))
time.sleep(1)
self.read_until_command_prompt()
except:
print "*ERROR* connection has some problem"
raise EOFError
while out != '':
time.sleep(0.2)
result += out
out = self.read()
return result
def start_command(self,command):
logging.info('*INFO* Execute command: {}', command)
self.write(command)
def put_file(self):
raise NotImplementedError
def get_file(self):
raise NotImplementedError
def set_timeout(self, timeout=''):
if timeout:
timeout, self._timeout = self._timeout, timestr_to_secs(timeout)
else:
timeout = secs_to_timestr(self._timeout)
return timeout
def set_prompt(self, prompt):
prompt_pool = [self._answer_prompt, self._shell_prompt, self._scli_prompt, self._custom_prompt]
old_prompt = '|'.join(filter(lambda item: item, prompt_pool))
(prompt, self._custom_prompt) = (self._custom_prompt, prompt)
return old_prompt
def set_newline(self, newline):
self._newline = newline.upper().replace('LF', '\n').replace('CR', '\r')
def reconnect_connection(self):
self._close()
# add this sleep as a workaround for a pronto: user "_xxxadmin" could not login when reconnecting, before some service get started
# time.sleep(120)
self._reconnect()
@property
def console_type(self):
return self._console_type
def read_until_no_messages(self):
data = ''
max_time = self._timeout + time.time()
retry = False
while time.time() < max_time:
_raw = self.read()
if _raw != '':
data += _raw
continue
else:
if not retry:
retry = True
time.sleep(0.5)
continue
else:
print data
data = self._decode(data)
return data
raise AssertionError("Read_until_no_msg Time Out")