-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsRemoteMachine.py
436 lines (383 loc) · 19 KB
/
WindowsRemoteMachine.py
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import os, sys, subprocess, re, time
import win32api, logging
from string import ascii_uppercase
from RemoteMachine import RemoteMachine
class WindowsRemoteMachine(RemoteMachine):
def __init__(self, machine_ip, machine_name, machine_username, machine_password, psexec_exe, enable_remoting=True, log_level='INFO'):
"""
constructor
:param machine_ip: ip address of remote machine
:param machine_name: host name of remote machine
:param machine_username: login username for remote machine
:param machine_password: login password for remote machine
:param psexec_exe: path to psexec exe
:param enable_remoting: flag to enable remoting while creating object
:param log_level: set log level
"""
# use 'super' to call a method defined in the parent class. The call below calls the constructor from
# parent class.
super(WindowsRemoteMachine, self).__init__(machine_ip, machine_name, machine_username, machine_password)
self.machine_type='windows'
self.psexec_exe=psexec_exe
# setup logging - change level to DEBUG to view all the activity
self.logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s: %(levelname)s: %(message)s', datefmt="%Y%m%d-%H%M%S")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.debug("Machine {0} of type {1} init done".format(self.machine_name, self.machine_type))
if enable_remoting:
self.enable_remoting()
if log_level:
self.set_log_level(log_level)
else:
self.set_log_level('INFO')
# --------------------------------------------------
# PROPERTIES
# --------------------------------------------------
# https://www.programiz.com/python-programming/property
@property
def ip(self):
"""
machine ip
:return: ip address of remote machine
"""
return self.machine_ip
@property
def name(self):
"""
machine host name
:return: hostname of remote machine
"""
return self.machine_name
@property
def type(self):
"""
machine os type
:return: os type of remote machine
"""
return self.machine_type
# --------------------------------------------------
# SETUP
# --------------------------------------------------
def enable_remoting(self):
"""
enable WinRM for remoting on the remote machine
:return: None
"""
self.logger.debug("Enable WinRM on machine {0}".format(self.machine_ip))
self._execute_psexec_command("powershell Enable-PSRemoting -Force")
def set_log_level(self, level):
"""
set the logging level of the class
:param level: log level to set for the object
:return: None
"""
if level == 'DEBUG':
self.logger.setLevel(logging.DEBUG)
elif level == 'INFO':
self.logger.setLevel(logging.INFO)
else:
self.logger.setLevel(logging.INFO)
# --------------------------------------------------
# CHECK METHODS
# --------------------------------------------------
def check_file_exists(self, file_path):
"""
check if the file exists on remote machine
:param file_path: path on the remote machine
:return: True/False
"""
file_path = file_path.replace(':','$')
file_path = "\\\\{0}\\{1}".format(self.machine_ip, file_path)
return os.path.exists(file_path) and os.path.isfile(file_path)
def check_folder_exists(self, folder_path):
"""
check if the folder exists on remote machine
:param folder_path: path on the remote machine
:return: True/False
"""
folder_path = folder_path.replace(':','$')
folder_path = "\\\\{0}\\{1}".format(self.machine_ip, folder_path)
return os.path.exists(folder_path) and os.path.isdir(folder_path)
# --------------------------------------------------
# SET METHODS
# --------------------------------------------------
def set_reg_key_value(self, path, key, value):
"""
set registry key on remote machine to value
:param path: path of key in registry editor
:param key: key name
:param value: key value
:return: output of powershell command executed
"""
self._execute_powershell_command("Set-ItemProperty -Path {0} -Name {1} -Value {2}".format(path, key, value))
# --------------------------------------------------
# GET METHODS
# --------------------------------------------------
def get_reg_key_value(self, path, key):
"""
get the registry value using specific key
:param path: path of key in registry editor
:param key: key name
:return: key value output of the powershell command
"""
output_rows = self._execute_powershell_command("Get-ItemProperty -Path {0} -Name {1}".format(path, key))
regex_string = r"({0})(\s*:\s*)(.*)".format(key)
match_obj = re.match(regex_string, output_rows[0])
if match_obj and match_obj.group(3):
return match_obj.group(3)
return None
# --------------------------------------------------
# DOWNLOAD
# --------------------------------------------------
def download_file(self, local_destination, remote_destination, file_name):
"""
download file from remote destination path to destination on local machine
:param local_destination: folder path on local machine
:param remote_destination: folder path on remote machine
:param file_name: file name to download
:return: True/False
"""
self.logger.debug("Downloading file {} FROM={}, TO={}".format(file_name, remote_destination, local_destination))
remote_destination_drive, remote_destination_subdir = os.path.splitdrive(remote_destination)
remote_destination_drive = remote_destination_drive.replace(':','$')
remote_destination_drive = os.path.join("\\\\{0}".format(self.machine_ip), remote_destination_drive)
map_drive = WindowsRemoteMachine.get_unused_drive()
self._run_system_command("net use {0} {1} /u:{2} {3}".format(map_drive, remote_destination_drive,
self.machine_username, self.machine_password))
remote_file_path = os.path.join(map_drive, '\\', remote_destination_subdir)
# check if remote file exists
if not os.path.exists(remote_file_path):
self.logger.error("file {} does not exist.".format(remote_file_path))
return False
# creates directory path if does not exist
self._execute_robocopy_command(remote_file_path, local_destination, file_name)
self._run_system_command("net use {0} /d".format(map_drive))
local_file_path=os.path.join(local_destination, file_name)
return os.path.exists(local_file_path) and os.path.isfile(local_file_path)
def download_folder(self, local_destination, remote_destination):
"""
download folder from remote destination to local destination
:param local_destination: path of folder on local machine
:param remote_destination: path of folder on remote machine
:return: true/false
"""
self.logger.debug("Downloading folder FROM={}, TO={}".format(remote_destination, local_destination))
remote_destination_drive, remote_destination_subdir = WindowsRemoteMachine.get_remote_destination_drive_path\
(self.machine_ip, remote_destination)
map_drive = WindowsRemoteMachine.get_unused_drive()
self._run_system_command("net use {0} {1} /u:{2} {3}".format(map_drive, remote_destination_drive,
self.machine_username, self.machine_password))
# creates directory path if does not exist
self._execute_robocopy_command(os.path.join(map_drive, '\\', remote_destination_subdir), local_destination)
self._run_system_command("net use {0} /d".format(map_drive))
return os.path.exists(local_destination) and os.path.isdir(local_destination)
# --------------------------------------------------
# UPLOAD
# --------------------------------------------------
def upload_file(self, local_destination, remote_destination, file_name):
"""
upload file from local destination to remote machine destination
:param local_destination: location on local machine
:param remote_destination: location on remote machine
:param file_name: name of file to upload
:return: true/false
"""
self.logger.debug("Uploading file {0} FROM={1}, TO={2}".format(file_name, local_destination, remote_destination))
remote_destination_drive, remote_destination_subdir = WindowsRemoteMachine.get_remote_destination_drive_path\
(self.machine_ip, remote_destination)
map_drive = WindowsRemoteMachine.get_unused_drive()
self._run_system_command("net use {0} {1} /u:{2} {3}".format(map_drive, remote_destination_drive, self.machine_username, self.machine_password))
# creates directory path if does not exist
self._execute_robocopy_command(local_destination, os.path.join(map_drive, '\\', remote_destination_subdir), file_name)
self._run_system_command("net use {0} /d".format(map_drive))
return self.check_file_exists(os.path.join(remote_destination,file_name))
def upload_folder(self, local_destination, remote_destination):
"""
upload folder from local destination to remote folder destination
:param local_destination: location on local machine
:param remote_destination: location on remote machine
:return: true/false
"""
self.logger.debug("Uploading folder FROM={0}, TO={1}".format(local_destination, remote_destination))
remote_destination_drive, remote_destination_subdir = WindowsRemoteMachine.get_remote_destination_drive_path \
(self.machine_ip, remote_destination)
map_drive = WindowsRemoteMachine.get_unused_drive()
self._run_system_command("net use {0} {1} /u:{2} {3}".format(map_drive, remote_destination_drive, self.machine_username, self.machine_password))
# creates directory path if does not exist
self._execute_robocopy_command(local_destination, os.path.join(map_drive, '\\', remote_destination_subdir))
self._run_system_command("net use {0} /d".format(map_drive))
return self.check_folder_exists(remote_destination)
# --------------------------------------------------
# SERVICE
# --------------------------------------------------
def service_status(self, service_name):
"""
check if the service by name is running on remote machine
:param service_name: name of service to check the status of
:return: true/false
"""
output_rows=self._execute_powershell_command("Get-Process {}".format(service_name))
if ((not output_rows) or (service_name not in output_rows[-1])):
return False
return True
def service_start(self, service_name, timeout=5):
"""
start service by name on remote machine
:param service_name: name of service to start
:param timeout: timeout for attempt to start
:return: true/false
"""
""" unable to do this reliably directly through powershell Start-Process or using task using name directly.
Only possible way is to use PsExec.exe with full process path """
pass
def service_stop(self, service_name, timeout=5):
"""
stop the service by name on remote machine
:param service_name: name of service to stop
:param timeout: timeout for service to stop
:return: true/false
"""
if self.service_status(service_name):
if '.exe' not in service_name:
service_name=service_name+'.exe'
self.logger.debug("Stopping service {}".format(service_name))
self.execute_command("taskkill /im {} /f".format(service_name))
else:
return True
end_time=time.time()+timeout
while end_time > time.time():
if self.service_status(service_name):
self.logger.debug("Waiting for service {} to stop".format(service_name))
time.sleep(1)
else:
self.logger.debug("Service {} stopped successfully".format(service_name))
return True
if self.service_status(service_name):
self.logger.debug("Unable to stop {} after timeout {} seconds expired".format(service_name, timeout))
return False
else:
self.logger.debug("Service {} started successfully".format(service_name))
return True
# --------------------------------------------------
# EXECUTE
# --------------------------------------------------
def execute_command(self, command):
"""
execute command on remote machine
:param command: command to execute on remote machine
:return: output of command execution
"""
return self._execute_psexec_command(command)
# --------------------------------------------------
# UTILS
# --------------------------------------------------
def _execute_powershell_command(self, command):
"""
execute powershell command using cmdlet on remote machine
:param command: powershell command to execute on remote machine
:return: output of powershell command executed
"""
file_name = 'remote_powershell_script.ps1'
content = "$sec_password=ConvertTo-SecureString \"{0}\" -AsPlainText -Force \n" \
"$my_creds=New-Object System.Management.Automation.PSCredential(\"{1}\",$sec_password) \n" \
"Invoke-Command -ComputerName {2} -Command {{{3}}} -Credential $my_creds " \
"\n".format(self.machine_password, self.machine_username, self.machine_name, command)
with open(file_name,'w') as fh:
fh.write(content)
output = self._run_system_command("powershell -ExecutionPolicy Bypass -File {0}".format(file_name)).split("\n")
os.remove(file_name)
return output
def _execute_robocopy_command(self, source, destination, file_name=None):
"""
execute robocopy command to copy file or folder from one location to other
:param source: location to copy from
:param destination: location to copy to
:param file_name: name of file to copy, if any
:return: None
"""
"""
robocopy error codes:
0x10 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to
insufficient access privileges on the source or destination directories.
0x08 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded).
Check these errors further.
0x04 Some Mismatched files or directories were detected. Examine the output log.
Housekeeping is probably necessary.
0x02 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
0x01 One or more files were copied successfully (that is, new files have arrived).
0x00 No errors occurred, and no copying was done. The source and destination directory trees are completely
synchronized.
"""
try:
if (file_name is None):
cmd = "robocopy \"{0}\" \"{1}\" /E".format(source, destination)
else:
cmd = "robocopy \"{0}\" \"{1}\" {2}".format(source, destination, file_name)
self._run_system_command(cmd)
except subprocess.CalledProcessError as ex:
if int(ex.returncode) == 1:
self.logger.debug("One or more files were copied successfully")
else:
raise
def _execute_psexec_command(self, command):
"""
execute command using PsExex.exe on remote machine
:param command: command to execute on remote machine
:return: None
"""
psexec_command = "{0} \\\\{1} -n 15 -u \"{2}\" -p \"{3}\" -h {4}".format(self.psexec_exe, self.machine_ip,
self.machine_username,
self.machine_password, command)
self._run_system_command(psexec_command)
def _run_system_command(self, cmd, sleep=None):
"""
run system command on local machine synchronously
:param cmd: command to execute on local machine
:param sleep: time to sleep after executing the command
:return: command output
"""
try:
self.logger.debug("CMD='{0}'".format(cmd))
output = subprocess.check_output(cmd, shell=True).strip()
if output:
self.logger.debug("CMD Output='{0}'".format(output))
if sleep:
time.sleep(sleep)
return str(output)
except subprocess.CalledProcessError as ex:
raise # raise all subprocess errors so that they can be handled appropriately by calling code
"""static methods are standalone. Neither 'self' or class instance is passed implicitly as the first argument.
They behave like plain functions. You can call below mehod directly from any python code without a class instance:
from WindowsRemoteMachine import WindowsRemoteMachine
print WindowsRemoteMachine.get_unused_drive()
"""
@staticmethod
def get_unused_drive():
"""
waits and gives an unmounted drive letter.
:return: string letter
"""
# wait loop if drive not found, so can run in parallel and queue up
drives = win32api.GetLogicalDriveStrings()
drives = drives.split(':\\\x00')[:-1]
while(True):
unused_drives = list()
for c in ascii_uppercase:
if c not in drives:
if not ((c is 'A') or (c is 'B') or (c is 'C') or (c is 'D')):
unused_drives.append(c)
if unused_drives:
return unused_drives[0]+":"
@staticmethod
def get_remote_destination_drive_path(ip, path):
"""
constructs remote path based on remote machine ip and path
:param ip: sample: 10.84.222.45
:param path: C:\\temp\\test
:return: \\10.84.222.45\C$, \temp\test
"""
drive, subdir = os.path.splitdrive(path)
drive = drive.replace(':','$')
return os.path.join("\\\\{0}".format(ip), drive), subdir