-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapNetworks.py
51 lines (43 loc) · 1.99 KB
/
mapNetworks.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
import os
import base64
import subprocess
class Drive:
def __init__(self, logger, networkPath="\\\\fgcz-ms.fgcz-net.unizh.ch\\Data2San",
user="FGCZ-NET\BioBeamer",
password="cGFzc3dvcmQ="):
self._networkPath = networkPath
self._user = user
self._password = base64.b64decode(password)
self._password = self._password.decode("utf-8")
self._logger = logger
def mapDrive(self):
winCMD = "net use {networkPath} {password} /user:{user}".format(
networkPath=self._networkPath,
password=self._password,
user=self._user)
self._logger.info("Attempt: net use {networkPath} <password> /user:{user}.".format(networkPath=self._networkPath,
user=self._user))
p = subprocess.Popen(winCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
err = err.decode("utf-8")
out = out.decode("utf-8")
if not err == "":
#self._logger.error(winCMD)
self._logger.info("Network drive {} for user {} can NOT be MAPPED.".format(self._networkPath, self._user))
self._logger.error(err.replace("\r\n", " "))
else:
self._logger.info(out.replace("\r\n", " "))
self._logger.info("Network drive {} for user {} mapped without error code.".format(self._networkPath, self._user))
return p.returncode
def unmapDrive(self):
winCMD = "net use {networkPath} /delete".format(
networkPath=self._networkPath)
self._logger.info(winCMD)
p = subprocess.Popen(winCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
err = err.decode("utf-8")
out = out.decode("utf-8")
if not err == "":
self._logger.error(winCMD)
self._logger.error(err)
return p.returncode