class SmbClient(object): def __init__(self,ip,username,password,sharename): self.ip = ip self.username = username self.password = password self.sharename = sharename def connect(self): self.server = SMBConnection(self.username, self.password,client,netbios_name,use_ntlm_v2=True) self.server.connect(self.ip,139) def upload(self,file): data = open(file,'rb') file = '/' + file self.server.storeFile(self.sharename,file,data) print "file has been uploaded" def download(self,file): fileobj = open(file,'wb') self.server.retrieveFile(self.sharename,fileobj) print "file has been downloaded in current dir" def delete(self,file): 'remove file from remote share' file = '/' + file self.server.deleteFiles(self.sharename,file) def list(self): ' list files of remote share ' filelist = self.server.listPath(self.sharename,'/') for f in filelist: print f.filename
def deleteFile(self,del_file_name):#删除文件 try: conn = SMBConnection(self.username, self.password, self.my_name, self.remote_smb_IP, self.domain_name, use_ntlm_v2=True,is_direct_tcp = True) conn.connect(self.remote_smb_IP,445) conn.deleteFiles(self.dir, self.display_path + '/' + del_file_name) except: conn.deleteDirectory(self.dir, self.display_path + '/' + del_file_name)
def delete_file(conn: SMBConnection, service_name: str, path_file_pattern: str): _file_abs_path = f"{service_name}/{path_file_pattern}" try: conn.deleteFiles(service_name, path_file_pattern) except Exception as e: logger.warning(f"删除文件失败,文件不存在:{_file_abs_path}") else: logger.info(f"删除文件成功:{_file_abs_path}")
def deleteFile(self): try: conn = SMBConnection(self.username.get(), self.password.get(), self.my_name, self.domain_name, use_ntlm_v2=True) conn.connect(self.remote_smb_IP.get(), int(self.port.get())) conn.deleteFiles( self.dir, self.display_path + '/' + ml.get(ml.curselection())) except: conn.deleteDirectory( self.dir, self.display_path + '/' + ml.get(ml.curselection()))
def delete_remote_samba_file(file_name): try: conn = SMBConnection(SMB_USER, SMB_PASSWORD, SMB_CLIENT_HOSTNAME, SMB_SERVER_NAME, domain=SMB_DOMAIN_NAME, use_ntlm_v2=True, is_direct_tcp=True) conn.connect(SMB_SERVER_IP, SMB_PORT) conn.deleteFiles(SMB_SHARED_FOLDER, file_name) return True except: return False
class SMBUtils: """ 访问 smb共享的通用功能 """ def __init__(self, host, username, password, remote_name, local_name=None): local_name = local_name if local_name else '' self.conn = SMBConnection(username, password, local_name, remote_name, is_direct_tcp=True) self.conn.connect(host, 445) def list_files(self, service_name, share_path, pattern="*"): """ 获得共享目录下的文件 """ result = [] filenames = self.conn.listPath(service_name, share_path, pattern=pattern) for filename in filenames: file_name, is_dir = filename.filename, filename.isDirectory if not is_dir: result.append(file_name) return result def download_file(self, shared_dir, shared_file_path, local_data_dir, local_file_name): """ 下载 windows共享目录 shared_dir下的 shared_file_path 文件到本地 local_data_dir 目录下, 保存为 local_file_name """ local_file_path = path_join(local_data_dir, local_file_name) with open(local_file_path, 'wb') as f: self.conn.retrieveFile(shared_dir, shared_file_path, f) def delete_files(self, shared_dir, path_file_pattern): """ 删除远程数据 """ self.conn.deleteFiles(shared_dir, path_file_pattern) def __del__(self): self.conn.close()
class FileServerConnection(object): """Connection to a Samba file server""" def __init__(self, ip, port, clientName, serverName, username, password): self.conn = SMBConnection(username, password, clientName, serverName) self.conn.connect(ip, port) try: shares = self.conn.listShares() sharesStr = ", ".join("{0.name} ({0.comments})".format(s) for s in shares) logging.info("Visible shares on {} ({}:{}): {}".format(serverName, ip, port, sharesStr)) except smb.base.NotReadyError as e: raise FileServerException(e) def append(self, share, path, data): try: # Get the existing contents of the file. file = StringIO() try: self.conn.retrieveFile(share, path, file) except smb.smb_structs.OperationFailure as e: # The file might not exist yet. if not e.message.endswith("Unable to open file"): # Something else went wrong. raise # Append the data. file.write(data) file.seek(0) # NOTE: Apparently storeFile fails if the target file exists. It # must be deleted first. # TODO: Rename the old file instead of deleting until the store # operation is completed succesfully? try: self.conn.deleteFiles(share, path) except smb.smb_structs.OperationFailure as e: # The file might not exist yet. if not e.message.endswith("Delete failed"): # Something else went wrong. raise self.conn.storeFile(share, path, file) except smb.smb_structs.OperationFailure as e: raise FileServerException(e.message)
def smbtestdir(session_name): fullname = 'fxb04fs0301.filex.com' ip = socket.gethostbyname(fullname) hostname, domain = fullname.split('.', 1) share = 'everyone' directory = '{}'.format(session_name) con = SMBConnection('', '', 'client', hostname) con.connect(ip) con.createDirectory(share, directory) con.close() yield fullname, share, directory con = SMBConnection('', '', 'client', hostname) con.connect(ip) for path, dirs, files in walk(con, share, directory, topdown=True): for filename in files: con.deleteFiles(share, '{}\\{}'.format(path, filename)) con.deleteDirectory(share, path) con.close()
def rollback(ticket_id): main, samba = set_samba_settings() conn = SMBConnection(samba['smb_username'], samba['smb_password'], os.environ['COMPUTERNAME'], getBIOSName(samba['smb_server']), use_ntlm_v2 = True) conn.connect(samba['smb_server'], 139) action_log = [] with open((main['local_storage']+delimiter()+str(ticket_id)+delimiter()+'action.log'), 'r') as log: for line in log.readlines(): action_log.append(line.strip()) action_log.reverse() notes = open(main['local_storage']+delimiter()+str(ticket_id)+delimiter()+'notes.txt', 'a+') notes.write("-"*54+" Rollback all change"+"-"*54+"\n") notes.write('%-75s\t%9s\t%s\n%s\n' % ('Remote filename', 'Action', 'Timestamp', '-'*128)) for line in action_log: line = line.split(',') # generate path for smb path = '\\'.join(line[1][2:].split('\\')[2:]) if line[0] == 'cp': conn.deleteFiles('c$', path) print "[INFO]\t"+datetime.now().strftime('%H:%M:%S %d-%m-%y')+"\tFile deleted: "+line[1] if line[0] == 'mkdir': conn.deleteDirectory('c$', path) print "[INFO]\t"+datetime.now().strftime('%H:%M:%S %d-%m-%y')+"\tDirectory deleted: "+line[1] notes.write('%-75s\t%9s\t%s\n' % (line[1], 'Deleted', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) os.remove(main['local_storage']+delimiter()+str(ticket_id)+delimiter()+'action.log') path = main['local_storage']+delimiter()+str(ticket_id)+delimiter()+'backup'\ + delimiter()+'trunk'+delimiter() + 'maximotest' tree = [] for item in os.walk(path): item = (item[0].replace(path, ''), item[1], item[2]) if item[0].replace(path, '')[1:8] == 'SOURCES': continue tree.append(item) for item in tree: if len(item[2]) > 0: for f in item[2]: f_path = 'maximo'+item[0]+delimiter() obj_file = str(path)+str(item[0])+delimiter()+str(f) file_size = conn.storeFile('c$', f_path+f, open(obj_file, 'rb')) file_path = delimiter()+delimiter()+str(samba['smb_server'])+delimiter()+"c$"+delimiter()+f_path+f print "[INFO]\t"+datetime.now().strftime('%H:%M:%S %d-%m-%y')+"\tFile restored from backup: "+str(file_path) notes.write('%-75s\t%9s\t%s\n' % (file_path, 'Restored', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) conn.close() return 0
class Nas: def __init__(self, config, connection): conf = config[connection] self.ip = conf['ip'] self.connection = SMBConnection(conf['user'], conf['password'], conf['local_name'], conf['nas_name']) self.connection.connect(self.ip) def set_root_directory(self, root): self.root = root def list_directory(self, path): return self.connection.listPath(self.root, path) def list_directory_names(self, path): dir_raw = self.list_directory(path) return [x.filename for x in dir_raw] def create_dir(self, path): self.connection.createDirectory(self.root, path) def download_file(self, origin, destination): with open(destination, 'wb') as download_file: # downloading file self.connection.retrieveFile(self.root, origin, download_file) def download_remote(self, path): filename = os.path.basename(path) file_obj = BytesIO() self.connection.retrieveFile(self.root, path, file_obj) return file_obj def upload_remote(self, path, file_obj): self.connection.storeFile(self.root, path, file_obj) def delete_file(self, path): self.connection.deleteFiles(self.root, path) def rename_path(self, origin, destination): self.connection.rename(origin, destination) def close(self): self.connection.close()
def check_ip(ip): global timeout, verbose, user, password, domain, print_lock, debug try: # Connect to socket conn = SMBConnection(user, password, "detect_unsecure_admin_share.py", ip, domain=domain, use_ntlm_v2=True, is_direct_tcp=True) assert conn.connect(ip, 445, timeout=timeout) if debug: with print_lock: print("#DEBUG: Successfully connected to ip: {}".format(ip)) f = tempfile.TemporaryFile() f.write("Hello World!\n") try: conn.storeFile("C$", "detect_unsecure_admin_share.tmp", f, timeout=timeout) with print_lock: print("#SUCCESS: Successfully stored test file on C$ admin share at ip: {}".format(ip)) conn.deleteFiles("C$", "detect_unsecure_admin_share.tmp", timeout=timeout) if debug: with print_lock: print("#DEBUG: Successfully deleted test file from C$ admin share at ip: {}".format(ip)) except Exception as ex: if debug: with print_lock: print("#ERROR: Could not store file on C$ admin share on ip: {}".format(ip)) finally: conn.close() f.close() except socket.timeout: if debug: with print_lock: print("#DEBUG: Connection timed out for ip: {}".format(ip)) except Exception as ex: if debug: with print_lock: print("#DEBUG: Connection failure for ip: {}".format(ip))
class Samba(): # 参数为用户、密码、本地计算机名、Samba计算机名、Samba地址、Samba端口 def __init__(self, user, passwd, localhost, remotehost, address, port): self.conn = SMBConnection(user, passwd, localhost, remotehost) self.status = self.conn.connect(address, port) def Time_Remove(self): if self.status: for file in self.conn.listPath('samba', 'backup')[2:]: if round((time.time() - file.create_time) / 3600 / 24) > 30: self.conn.deleteFiles('samba', 'backup/{0}'.format(file.filename)) shutil.rmtree(Home_path) # 上传文件到backup目录下 def upload(self, localfile): with open(localfile[0], 'rb') as up: self.conn.storeFile( 'samba', 'backup/{0}'.format(os.path.basename(localfile[0])), up) def close(self): self.conn.close() print('上传到Samba服务器成功,请登录查看。')
class SmbClient: """Class for interaticng with remote Samba/smb server """ def __init__(self, server_ip, username, password, share_name): """Initialize the smb class Parameters ---------- server_ip : str IP address of the smb server username : str Username for the smb server password : str Password for the smb server share_name : str Root shared folder name on the smb server """ # smb attributes self.server_ip = server_ip self.username = username self.password = password self.share_name = share_name self.port = 139 # Hostname of the local machine self.client_name = subprocess.Popen(['hostname'],stdout=subprocess.PIPE).communicate()[0].strip().decode('UTF-8') logging.info("SMBClient initialized successfully") def connect(self): """Function to create a connection with smb server """ self.server = SMBConnection( username = self.username, password = self.password, my_name = self.client_name, remote_name = self.share_name ) self.server.connect(self.server_ip, self.port) logging.info("Connection to smb created") def upload(self, file_name, file_location_local, file_location_smb): """Function to upload file on smb server from local Parameters ---------- file_name : str Name of the file that needs to be copied file_location_local : str Folder location where file exists on local server file_location_smb : str Folder location relative to share name where file exists on smb server """ file_path_local = os.path.join(file_location_local, file_name) file_path_smb = os.path.join(file_location_smb, file_name) data = open(file_path_local, 'rb') self.server.storeFile(self.share_name, file_path_smb, data) logging.info(f"File [{file_name}] has been uploaded") def download(self, file_name, file_location_local, file_location_smb): """Function to download file from smb server to local Parameters ---------- file_name : str Name of the file that needs to be downloaded file_location_local : str Folder location where file exists on local server file_location_smb : str Folder location relative to share name where file exists on smb server """ file_path_local = os.path.join(file_location_local, file_name) file_path_smb = os.path.join(file_location_smb, file_name) with open(file_path_local, 'wb') as fp: self.server.retrieveFile(self.share_name, file_path_smb, fp) logging.info(f"File [{file_name}] has been downloaded") def delete(self, file_name, file_location_smb): """Function to delete a file on smb server Parameters ---------- file_name : str Name of the file that needs to be deleted file_location_smb : str Folder location relative to share name where file exists on smb server """ file_path = os.path.join(file_location_smb, file_name) self.server.deleteFiles(self.share_name, file_path) logging.info(f"File [{file_name}] has been deleted") def list_files(self, files_location_smb): """Function to get the list of files from a folder on SMB server Parameters ---------- files_location_smb : [type] Folder location relative to share name where files exists on smb server Returns ------- list[str] List of files in the given smb folder """ filelist = self.server.listPath(self.share_name, files_location_smb) file_name_list = [] for f in filelist: file_name_list.append(f.filename) file_name_list = [ file_name for file_name in file_name_list if file_name not in ['.', '..']] logging.info("File list has been fetched") return file_name_list
class SMBClient: def __init__(self, serverOrIP:str=None, username:str=None, password:str=None, service_name:str=None): if self.isIP4(serverOrIP): self._ip = serverOrIP self._servername = '' else: self._servername = serverOrIP self._ip = None if serverOrIP is None else self.__resolve__(serverOrIP) self._username = username self._password = password self._port = SMB_PORT self._service_name = service_name # service_name is the share self._server = None self._host = self.__get_localhost__() def isIP4(self, serverOrIP): # If no valid IP4 adres, try to obtain it validation = "^()|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$" return ( serverOrIP != None and len(serverOrIP) > 0 and isinstance(serverOrIP, str) and serverOrIP.count('.') == 3 and re.match(validation, serverOrIP) ) def __resolve__(self, server:str): # str | None # If no valid IP adres, try to obtain it # First as netBIOS on LAN nb = NetBIOS(broadcast=True, listen_port=0) # Send a query on the network and hopes that if machine matching the name will reply with its IP address. res = nb.queryName(server, ip='', port=NETBIOS_PORT, timeout=NETBIOS_TIMEOUT) if isinstance(res, list) and len(res) > 0 and isinstance(res[0], str) and self.isIP4(res[0]): return res[0] # Try to DNS resolve try: return socket.gethostbyname(server) except socket.gaierror as sge: # Nope, didn't work return None """ returns con successful (bool) and a dict (json) with details catch values using *r = self.connect() or connected, details = self.connect() os simply connected = self.connect()[0] """ def connect(self): """ Connect and authenticate to the SMB share. """ self._server = SMBConnection(username=self._username, password=self._password, my_name=self._host, remote_name=self._servername, use_ntlm_v2=True) connectSuccess = False connectionRefused = False resolved = self._ip is not None if not resolved: return connectSuccess, {'resolved': resolved, 'connectionRefused': connectionRefused } try: connectSuccess = self._server.connect(self._ip, port=self._port, timeout=CONNECT_TIMEOUT) except ConnectionRefusedError as cre: connectionRefused = True except Exception as e: pass return connectSuccess, dict({'resolved': resolved, 'connectionRefused': connectionRefused }) def close(self): if self._server is not None: self._server.close() """ service_name (string/unicode) – the name of the shared folder for the path path (string/unicode) – path relative to the service_name where we are interested to learn about its files/sub-folders. search (integer) – integer value made up from a bitwise-OR of SMB_FILE_ATTRIBUTE_xxx bits (see smb_constants.py). pattern (string/unicode) – the filter to apply to the results before returning to the client. """ def list(self, serviceName:str=None, smbPath:str='/', pattern:str='*', search:int=65591, timeout:int=SEARCH_TIMEOUT): return self._server.listPath(service_name=serviceName if serviceName is not None else self._service_name, path=smbPath, search=search, pattern=pattern, timeout=timeout ) """ Retrieve a list of shared resources on remote server. Returns: A list of smb.base.SharedDevice instances describing the shared resource """ def listShares(self, timeout:int=SEARCH_TIMEOUT): if self._server is not None: return self._server.listShares(timeout=timeout) return None def download(self, service_name:str=None, local_path:str=None, remote_path:str=None, files=None): """ Download files from the remote share. """ files = [] if files is None else files for file in files: with open(os.path.join(local_path, file) if local_path is not None else file, 'rb') as file_obj: self._server.retrieveFile(service_name=self._service_name, path=os.path.join(remote_path, file) if remote_path is not None else file, file_obj=file_obj ) def upload(self, service_name:str=None, local_path:str=None, remote_path:str=None, files=None, timeout:int=UPLOAD_TIMEOUT) -> dict: """ Upload files to the remote share. """ files = [] if files is None else files try: for file in files: with open(os.path.join(local_path, file) if local_path is not None else file, 'rb') as file_obj: self._server.storeFile(service_name=service_name if service_name is not None else self._service_name, path=os.path.join(remote_path, file) if remote_path is not None else file, file_obj=file_obj, timeout=timeout ) except Exception as e: return { 'success': False, 'reason': str(e) } return { 'success': True } def deleteFiles(self, service_name:str=None, remote_path:str=None, files=None, delete_matching_folders:bool=False, timeout:int=UPLOAD_TIMEOUT) -> dict: """ Remove files from the remote share. """ files = [] if files is None else files try: for file in files: self._server.deleteFiles(service_name=service_name if service_name is not None else self._service_name, path_file_pattern=os.path.join(remote_path, file) if remote_path is not None else file, delete_matching_folders=delete_matching_folders, timeout=timeout ) except Exception as e: return { 'success': False, 'reason': str(e) } return { 'success': True } def __get_localhost__(self): return subprocess.check_output('hostname', shell=True).decode(sys.stdout.encoding).strip()
class EasyPySMB: def __init__( self, hostname, username="******", password=None, domain=None, client_name=None, port=139, share_name=None, file_path=None, netbios_name=None, nocheck=False, ): if hostname.startswith("smb://"): regex = "smb://(((.+);)?(.+):(.+)@)?([^/]+)(/([^/]+))?(/.*)?" m = re.match(regex, hostname) if not m: raise ValueError( "Could not decompose smb path. The regex failed.") domain = m.group(3) if m.group(3) else "" username = m.group(4) if m.group(4) else "GUEST" password = m.group(5) if m.group(5) else "" hostname = m.group(6) share_name = m.group(8) file_path = m.group(9) logger.debug( "Domain: {} Username: {} Password: {} Server: {} Share: {} " "File Path: {}".format(domain, username, password, hostname, share_name, file_path)) if not client_name: client_name = __name__ if not netbios_name: netbios_name = get_netbios_name(hostname) self.conn = SMBConnection( domain=domain, username=username, password=password, my_name=client_name, remote_name=netbios_name, use_ntlm_v2=True, ) if not self.conn.connect(hostname, port): logger.error("Could not connnect to SMB server. Please verify the " "connection data") # tmpdir is a temp dir that holds the transfered files by default self.tmpdir = tempfile.mkdtemp(prefix="easypysmb_") self.share_name = share_name self.file_path = file_path if not nocheck: if self.share_name: available_shares = [x.lower() for x in self.list_shares()] if self.share_name.lower() not in available_shares: logger.warning( "Share {} does not exist on the server".format( self.share_name)) if self.file_path: dir_content = [ x.filename for x in self.ls(os.path.dirname(self.file_path)) ] if os.path.basename(self.file_path) not in dir_content: logger.warning( "File {} does not exist on the server".format( self.file_path)) def __decompose_smb_path(self, path): """ Get the share name and filepath """ split_path = path.split("/") return split_path[0], "/".join(split_path[1:]) def __guess_share_name(self, path, share_name=None): if share_name: return share_name, path available_shares = [x.lower() for x in self.list_shares()] if not share_name: first_dir = path.split("/")[0].lower() if first_dir in available_shares: logger.info("Path {} matches share name {}".format( path, first_dir)) share_name, path = self.__decompose_smb_path(path) elif self.share_name: share_name = self.share_name return share_name, path def close(self): self.conn.close() def list_shares(self): return [x.name for x in self.conn.listShares()] def store_file(self, file_obj, dest_path=None, share_name=None, retries=3): if not dest_path: dest_path = self.file_path assert dest_path, "Destination path is unset" share_name, dest_path = self.__guess_share_name(dest_path, share_name) if type(file_obj) is str or type(file_obj) is str: file_obj = open(file_obj, "rb") logger.info("Attempt to store {} on {}:{}".format( file_obj.name, share_name, dest_path)) for r in range(1, retries + 1): try: return self.conn.storeFile(share_name, dest_path, file_obj) except Exception as e: logger.error( "Attempt {}/{} to store file on SMB share failed:\n{}". format(r, retries, e)) def retrieve_file(self, dest_path=None, file_obj=None, share_name=None): if not dest_path: dest_path = self.file_path assert dest_path, "Destination path is unset" share_name, dest_path = self.__guess_share_name(dest_path, share_name) if not file_obj: file_obj = open( os.path.join(self.tmpdir, os.path.basename(dest_path)), "w+b") # file_obj = tempfile.NamedTemporaryFile( # prefix='py_', # suffix=os.path.basename(dest_path), # dir=self.tmpdir, # delete=False # ) elif type(file_obj) is str or type(file_obj) is str: file_obj = open(file_obj, "wb") bytes_transfered = self.conn.retrieveFile(share_name, dest_path, file_obj) logger.info("Transfered {} bytes".format(bytes_transfered)) file_obj.close() # write file file_obj = open(file_obj.name) return file_obj def backup_file( self, backup_file_path, file_path=None, share_name=None, backup_share_name=None, ): if not file_path: file_path = self.file_path assert file_path, "Destination path is unset" share_name, file_path = self.__guess_share_name(file_path, share_name) backup_share_name, backup_file_path = self.__guess_share_name( backup_file_path, backup_share_name) logger.info("Back up file {}:{} to {}:{}".format( share_name, file_path, backup_share_name, backup_file_path)) tmp_file = tempfile.NamedTemporaryFile( suffix=os.path.splitext(file_path)[1], delete=False) tmp_file = self.retrieve_file(share_name=share_name, dest_path=file_path, file_obj=tmp_file) res = self.store_file( share_name=backup_share_name, dest_path=backup_file_path, file_obj=tmp_file, ) # Clean up os.remove(tmp_file.name) return res def mkdir(self, dir_path, share_name=None): share_name, dir_path = self.__guess_share_name(dir_path, share_name) # Recursively create directories, just like mkdir -p does directories = dir_path.split("/") tmp_path = "" for d in directories: dir_content = self.conn.listPath(share_name, tmp_path) if d not in [x.filename for x in dir_content if x.isDirectory]: logger.info("Directory {} is missing. Create it".format(d)) self.conn.createDirectory(share_name, "{}/{}".format(tmp_path, d)) tmp_path += "/{}".format(d) def rm(self, file_path, share_name=None): if not share_name: if self.share_name: share_name = self.share_name else: share_name, file_path = self.__decompose_smb_path(file_path) return self.conn.deleteFiles(share_name, file_path) def ls(self, path="", share_name=None): if not share_name: if self.share_name: share_name = self.share_name else: share_name, path = self.__decompose_smb_path(path) logger.info("List files in {}:{}".format(share_name, path)) return self.conn.listPath(share_name, path=path)
class SambaClient: connection = None client_id = socket.gethostname() def __init__(self, user_name, password, system_name, ip='', min=0, max=60, randomize=True): self.user_name = user_name self.password = password self.system_name = system_name self.ip = system_name if len(ip) > 0: self.ip = ip self.min = min self.max = max self.randomize = randomize self.connect() def connect(self): print('Connecting to server with {} {} {} {} {}'.format( self.user_name, self.password, self.client_id, self.system_name, self.ip)) self.connection = SMBConnection(self.user_name, self.password, self.client_id, self.system_name) print('Connecting to server...') # establish the actual connection connected = self.connection.connect(ip=self.ip, port=139) if not connected: print('Failed to init connection.') else: print('Connection initialized.') def list_shares(self): try: response = self.connection.listShares() names = [] for r in response: names.append(r.name) print('Shares: {}'.format(names)) self.wait() return response except Exception as ex: print(ex) def list_directory(self, share, path='/'): try: response = self.connection.listPath(share.name, path) names = [] for r in response: names.append(r.filename) print('Files: {}'.format(names)) self.wait() return response except Exception as ex: print(ex) def download_file(self, share, path): file = open(os.devnull, "wb") try: info = self.connection.retrieveFile(share.name, path, file) file.close() print('Downloaded ' + str(info[1]) + ' bytes.') self.wait() except Exception as ex: print(ex) def upload_file(self, share, path, file): try: self.connection.storeFile(share.name, path, file) print('File {} uploaded.'.format(path)) self.wait() except Exception as ex: print(ex) def delete_file(self, share, path): try: self.connection.deleteFiles(share.name, path) print('Deleted file {}'.format(path)) self.wait() except Exception as ex: print(ex) def wait(self): if self.randomize: interval = random.randint(self.min, self.max) else: interval = (self.max - self.min) / 2 time.sleep(interval)
def SMB_Connect(host,sharename,user,password,folder,writeable): '''connects to a share with the given credentials and checks if it's writeable or not host: hostname (FQDN) sharename: Name of the share username: username password: password writeable: if set to True, it will check if the share is writeable ''' check_passed=False check_file=''.join(['/', folder,'/nagioscheck.txt']) hostname = host.split('.') host_ip= socket.gethostbyname(host) conn = SMBConnection(user, password, socket.gethostname(), hostname[0], use_ntlm_v2 = True) try: conn.connect(host_ip, 139) except: print "Connection to Host failed" sys.exit(status['CRITICAL']) if conn.auth_result: #only check if share is listed if not writeable: shares = conn.listShares() for share in shares: if sharename == share.name: print "Found ",share.name check_passed = True break else: #schreiben check_value = "File Created from nagios "+str(datetime.now()) file_obj = tempfile.NamedTemporaryFile() file_obj.write(check_value) file_obj.flush() file_obj.seek(0) try: conn.storeFile(sharename, check_file, file_obj) except: check_passed=False file_obj.close() #lesen file_obj = tempfile.NamedTemporaryFile() try: file_attributes, filesize = conn.retrieveFile(sharename, check_file, file_obj) file_obj.seek(0) file_content= file_obj.read() if file_content == check_value: check_passed=True except: check_passed=False file_obj.close() conn.close() #file loeschen try: conn = SMBConnection(user, password, socket.gethostname(), hostname[0], use_ntlm_v2 = True) conn.connect(host_ip, 139) conn.deleteFiles(sharename, check_file) except Exception, e: check_passed=False conn.close()
class SMBNetDriver(StorageDriver): "Implement a SMB driver " scheme = "smb" #https://pythonhosted.org/pysmb/api/smb_SMBConnection.html def __init__(self, mount_url=None, credentials = None, readonly = False, **kw): """ initializae a storage driver @param mount_url: optional full storeurl to mount """ self.conn = None self.mount_url = posixpath.join(mount_url, '') self.readonly = readonly if credentials is None: log.warn ("SMBMount Cannot proceed without credentials") return self.user, self.password = credentials.split (':') self.localhost = socket.gethostname() urlcomp = urlparse.urlparse (self.mount_url) self.serverhost = urlcomp.netloc self.server_ip = socket.gethostbyname(self.serverhost) # New interface def mount(self): """Mount the driver @param mount_url: an smb prefix to be used to mount smb://smbhostserver/sharename/d1/d2/" @param credentials: a string containing user:password for smb connection """ # I don't this this is the SMB hostname but am not sure if self.conn is not None: return self.conn = SMBConnection (self.user, self.password, self.localhost, self.serverhost) if not self.conn.connect(self.server_ip, 139): self.conn = None #except smb.base.NotReadyError: # log.warn("NotReady") #except smb.base.NotConnectedError: # log.warn("NotReady") #except smb.base.SMBTimeout: # log.warn("SMBTimeout") def unmount (self): """Unmount the driver """ if self.conn: self.conn.close() self.conn=None def mount_status(self): """return the status of the mount: mounted, error, unmounted """ @classmethod def split_smb(cls,storeurl): "return a pair sharename, path suitable for operations" smbcomp = urlparse.urlparse (storeurl) # smb://host smbcomp.path = /sharenmae/path _, sharename, path = smbcomp.path.split ('/', 2) return sharename, '/' + path # File interface def valid (self, storeurl): "Return validity of storeurl" return storeurl.startswith (self.mount_url) and storeurl def push(self, fp, storeurl, uniq=None): "Push a local file (file pointer) to the store" sharename, path = self.split_smb(storeurl) uniq = uniq or make_uniq_code() base,ext = os.path.splitext(path) for x in xrange(len(uniq)-7): try: if not self.conn.getAttributes (sharename, path): break except smb.OperationError: path = "%s-%s%s" % (base , uniq[3:7+x] , ext) written = self.conn.storeFile (sharename, path, fp) log.debug ("smb wrote %s bytes", written) return "smb://%s/%s" % (sharename, path), None def pull (self, storeurl, localpath=None, blocking=True): "Pull a store file to a local location" sharename, path = self.split_smb(storeurl) if self.conn: if localpath: file_obj = open (localpath, 'wb') else: file_obj = tempfile.NamedTemporaryFile() file_attributes, filesize = self.conn.retrieveFile(sharename, path, file_obj) log.debug ("smb fetch of %s got %s bytes", storeurl, filesize) def chmod(self, storeurl, permission): """Change permission of """ def delete(self, storeurl): 'delete an entry on the store' sharename, path = self.split_smb(storeurl) if self.conn: self.conn.deleteFiles(sharename, path) def isdir (self, storeurl): "Check if a url is a container/directory" def status(self, storeurl): "return status of url: dir/file, readable, etc" def list(self, storeurl): "list contents of store url"
class CSamba: def __str__(self): return 'CSamba: Modularize Samba connection.' def __init__(self): self.isConnect = False self.handle = None def connect(self, s_ip, s_id, password, client_name, server_name, domain): is_connect = False self.handle = SMBConnection(s_id, password, client_name, server_name, domain, use_ntlm_v2=True) try: is_connect = self.handle.connect(s_ip, 139) except: print('handle connect error') return is_connect def append_log(self, f_name, byte): with open('/var/log/dgmelog', 'a') as f: f.write('[{0:^24}] [{1:<15}]: [{2:>8}] bytes downloaded.\n'.format( datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), f_name, byte)) def download(self, folder): ret = True local_path = '/home/rp/TypeE' try: file_list = self.handle.listPath(folder, '/') except: ret = False print('list path error') else: for file in file_list: if not file.isDirectory: local_filename = '{}/{}'.format(local_path, file.filename) remote_filename = '/{}'.format(file.filename) try: is_exists = Path(local_filename).exists() except: print('Path file exists error') else: if is_exists: #check local file size f_size = Path(local_filename).stat().st_size print('f_size = ', f_size) if f_size > 0: # print('{} exist.'.format(file.filename)) try: self.handle.deleteFiles( folder, remote_filename) except: print('delete error(exists)') else: with open(local_filename, 'wb') as f: try: file_attr, file_size = self.handle.retrieveFile( folder, remote_filename, f) print( '====Download Finished====\n{}\n{}' .format(file_attr, file_size)) except: ret = False print('remote error') break else: self.append_log( file.filename, file_size) #check local file size try: f_size = Path( local_filename).stat().st_size except FileNotFoundError as e: print('FileNotFoundError:', e) else: if f_size > 0: try: self.handle.deleteFiles( folder, remote_filename) except: print('delete error') else: f_size = Path(local_filename).unlink() else: with open(local_filename, 'wb') as f: try: file_attr, file_size = self.handle.retrieveFile( folder, remote_filename, f) print('====Download Finished====\n{}\n{}'. format(file_attr, file_size)) except: ret = False print('remote error') break else: self.append_log(file.filename, file_size) #check local file size try: f_size = Path(local_filename).stat().st_size except FileNotFoundError as e: print('FileNotFoundError:', e) else: if f_size > 0: try: self.handle.deleteFiles( folder, remote_filename) except: print('delete error') else: f_size = Path(local_filename).unlink() run([ 'touch', '/home/rp/.data_gather/Core/connectivity.pk' ]) return ret def close(self): self.handle.close()
class SMBClient: """ SMB连接客户端 """ status = False samba = None def __init__(self, username: str, password: str, ip: str, port: int = 139): self.username = username self.password = password self.ip = ip self.port = port self.samba = SMBConnection(self.username, self.password, '', '', use_ntlm_v2=True) self.samba.connect(self.ip, self.port) self.status = self.samba.auth_result def close(self): if self.status: self.samba.close() def list_smb_dir(self, share_name, sub_dir=""): """列出文件夹内所有文件名 :param share_name: 共享文件夹名称 :param sub_dir: 相对共享文件夹的子目录 """ file_names = list() for e in self.samba.listPath(share_name, sub_dir): if e.filename[0] != '.': # 过滤上级文件夹及影藏文件 file_names.append(e.filename) return file_names def download(self, filename, local_dir, share_name, sub_dir=""): """下载文件 :param filename: 文件名 :param share_name: 共享文件夹名称 :param sub_dir: 相对共享文件夹的子目录 :param local_dir: 本地保存文件夹路径 """ assert isinstance(filename, str) with open(os.path.join(local_dir, filename), 'wb') as fp: self.samba.retrieveFile(share_name, os.path.join(sub_dir, filename), fp) def download_bytes(self, share_name, sub_file_path): """直接下载共享文件的Bytes数据 :param share_name: 共享文件夹名称 :param sub_file_path: 共享文件路径 """ fp = io.BytesIO() self.samba.retrieveFile(share_name, sub_file_path, fp) fp.seek(0) data = fp.read() fp.close() return data def upload(self, local_file_path, share_name, smb_save_path): """上传文件 :param local_file_path: 本地文件路径 :param share_name: 共享文件夹名称 :param smb_save_path: 在共享文件夹的存放路径 """ with open(local_file_path, "rb") as fp: self.samba.storeFile(share_name, smb_save_path, fp) def upload_bytes(self, data, share_name, smb_save_path): """直接将Bytes类型的数据保存到共享文件 :param data: Bytes类型数据 :param share_name: 共享文件夹名称 :param smb_save_path: 在共享文件夹的存放路径 """ fp = io.BytesIO(data) self.samba.storeFile(share_name, smb_save_path, fp) fp.close() def create_dir(self, share_name, sub_dir): """创建文件夹 :param share_name: 共享文件夹名称 :param sub_dir: 相对共享文件夹的子目录 """ self.samba.createDirectory(share_name, sub_dir) def file_attrs(self, share_name, smb_file_path): attrs = {} try: file = self.samba.getAttributes(share_name, smb_file_path) for attr in ['alloc_size', 'create_time', 'file_attributes', 'file_id', 'file_size', 'filename', 'isDirectory', 'isNormal', 'isReadOnly', 'last_access_time', 'last_attr_change_time', 'last_write_time', 'short_name']: attrs[attr] = getattr(file, attr) except OperationFailure: pass return attrs def delete_file(self, share_name, smb_file_pattern): """删除文件, 默认都把传入的smb_file_pattern转换为Unicode字符串 :param share_name: 共享文件夹名称 :param smb_file_pattern: 文件的相对路径, 可以使用通配符 """ self.samba.deleteFiles(share_name, smb_file_pattern)
# Actually Deleting SharedRecord item.delete() #deleting files for item in flist: flag = 0 # flag 0 means its currently not used in any sharedrecord for record in temp: if record.copyname == str(item): flag = 1 # d = SHARED_FOLDER + item # If flag gets 0 means that file in SharedFolder is not linked to any SHaredRecord in database and must be deleted from server if flag == 0: print(item) # Deleting File from SharedFolder # 'sharedfiles' is name of Share which is written in smb.conf file conn.deleteFiles('sharedfiles', '/' + item) # Writing Log for delete file text = str( now.replace(microsecond=0) ) + ' ' + 'System' + ' Deleted_File ' + item + ' ' + '-' + ' ' + '-' write_log(text, logfile) # Closing the Samba Connection conn.close()
class Samba: def connect(self): self._conn = SMBConnection(self.remote_username, self.remote_password, self.local_machine_alias, self.remote_machine_name, use_ntlm_v2=True) assert self._conn.connect(self.remote_ip_address, 139) def __init__(self, remote_username, remote_password, local_machine_alias, remote_machine_name, remote_ip_address): self.remote_username = remote_username self.remote_password = remote_password self.local_machine_alias = local_machine_alias self.remote_machine_name = remote_machine_name self.remote_ip_address = remote_ip_address self.connect() """ remote_file_path: started with '/', related to remote_shared_folder_name for example: fetch_file(/src/tce/'rm_data_from_invoke_xxxxxxxx'.csv, 'sff_db', '/nj/rm_data_from_invoke_xxxxxxxx.csv') """ def fetch_file(self, local_file_path, remote_shared_folder_name, remote_file_path, replace=False): self.connect() local_folder = os.path.dirname(local_file_path) if not os.path.exists(local_folder): os.makedirs(local_folder) if not replace and os.path.isfile(local_file_path): raise Exception(f'Local file exists: {local_file_path}') else: with open(local_file_path, 'wb') as fp: file_attributes, filesize = self._conn.retrieveFile( remote_shared_folder_name, remote_file_path, fp, timeout=30) return file_attributes, filesize """ remote_file_path: started with '/', related to remote_shared_folder_name """ def delete(self, remote_shared_folder_name, remote_file_path_pattern, delete_matching_folders=False): self.connect() self._conn.deleteFiles(remote_shared_folder_name, remote_file_path_pattern, delete_matching_folders=delete_matching_folders, timeout=30) """ Return: A list of smb.base.SharedFile instances remote_file_path: started with '/', related to remote_shared_folder_name """ def listPath(self, remote_shared_folder_name, remote_file_path_pattern): self.connect() return self._conn.listPath(remote_shared_folder_name, remote_file_path_pattern)
def SMB_Connect(host, sharename, user, password, folder, writeable): '''connects to a share with the given credentials and checks if it's writeable or not host: hostname (FQDN) sharename: Name of the share username: username password: password writeable: if set to True, it will check if the share is writeable ''' check_passed = False check_file = ''.join(['/', folder, '/nagioscheck.txt']) hostname = host.split('.') host_ip = socket.gethostbyname(host) conn = SMBConnection(user, password, socket.gethostname(), hostname[0], use_ntlm_v2=True) try: conn.connect(host_ip, 139) except: print "Connection to Host failed" sys.exit(status['CRITICAL']) if conn.auth_result: #only check if share is listed if not writeable: shares = conn.listShares() for share in shares: if sharename == share.name: print "Found ", share.name check_passed = True break else: #schreiben check_value = "File Created from nagios " + str(datetime.now()) file_obj = tempfile.NamedTemporaryFile() file_obj.write(check_value) file_obj.flush() file_obj.seek(0) try: conn.storeFile(sharename, check_file, file_obj) except: check_passed = False file_obj.close() #lesen file_obj = tempfile.NamedTemporaryFile() try: file_attributes, filesize = conn.retrieveFile( sharename, check_file, file_obj) file_obj.seek(0) file_content = file_obj.read() if file_content == check_value: check_passed = True except: check_passed = False file_obj.close() conn.close() #file loeschen try: conn = SMBConnection(user, password, socket.gethostname(), hostname[0], use_ntlm_v2=True) conn.connect(host_ip, 139) conn.deleteFiles(sharename, check_file) except Exception, e: check_passed = False conn.close()
def handle_JobMessage(self, msg): try: self.errors = "" def set_status(s): self.status = s self.log.info(s) set_status("Received job") sim = msg.get_property("msg") user_id = sim["user_id"] sim_id = sim["sim_id"] job_id = sim["job_id"] sample = sim["sample"] jar_hash = sim["jar_hash"] set_status("Starting job %d %d %s %d" % (job_id, sim_id, user_id, sample)) self.send_status("Starting job", job_id, sim_id, user_id, sample) out_name = os.path.join("results", self.worker_id) jar_name = os.path.join("jars", "%s_%s.run" % (jar_hash, self.worker_id)) xml_name = os.path.join("xmls", "%s_%i_%i_%i.xml" % (user_id, job_id, sim_id, sample)) set_status("Writing input files") self.send_status("Writing input files", job_id, sim_id, user_id, sample) xml = construct_xml(sim, out_name) if not xml: self.errors = "Error constructing XML (check idrefs?)" set_status(self.errors) raise Exception(self.errors) with open(xml_name, "w") as xml_file: xml_file.write(xml) if not os.path.exists(jar_name): with open(jar_name, "wb") as jar_file: jar_file.write(get_file(jar_hash)) process = Popen(["java", "-server", "-Xmx2400M", "-jar", jar_name, xml_name], stdout=PIPE, stderr=PIPE) p_timer = time.time() # Non-blocking process io: http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python def enqueue_output(stream, queue, running): while running.value: queue.put(stream.read(128)) def create_thread_queue(stream, running): q = Queue() t = Thread(target=enqueue_output, args=(stream, q, running)) t.daemon = True t.start() return q running = Value("b", True) out_q = create_thread_queue(process.stdout, running) err_q = create_thread_queue(process.stderr, running) set_status("Execution started") while process.poll() == None: try: status = out_q.get(timeout=0.1) if time.time() - p_timer > 0.3: s = re.findall("\(.*?\)", status)[-1] self.send_status(s, job_id, sim_id, user_id, sample) p_timer = time.time() except: pass # Stop the queue threads running.value = False # Get the error if it exists, only needed here because thread is constantly checking the pipe while not err_q.empty(): self.errors += err_q.get(False) os.remove(xml_name) set_status("Execution finished") if self.errors: set_status(self.errors) raise Exception("CIlib error") set_status("Posting results") with open(out_name, "r") as result_file: conn = SMBConnection(SMB_USER, SMB_PWD, "", "", use_ntlm_v2=True) assert conn.connect(SMB_IP, timeout=SMB_TIMEOUT) newFile = "%s_%i_%i_%i.txt" % (user_id, job_id, sim_id, sample) existingFiles = [i.filename for i in conn.listPath(SMB_SHARE, "/")] if newFile in existingFiles: conn.deleteFiles(SMB_SHARE, newFile, timeout=SMB_TIMEOUT) conn.storeFile(SMB_SHARE, newFile, result_file, timeout=SMB_TIMEOUT) conn.close() os.remove(out_name) set_status("Result notification") self.connect() self.mgr.broadcast_message( ResultMessage( msg={ "job_id": job_id, "sim_id": sim_id, "user_id": user_id, "sample": sample, "jar_hash": jar_hash, "worker_id": self.worker_id, } ) ) set_status("Status update") self.send_status("Done", job_id, sim_id, user_id, sample) set_status("Job Done") except error: self.log.info("con error") self.connect() self.send_job_request() # TODO: and then? did that ^ fix it? except Exception, e: try: self.log.exception("Worker job error") self.log.exception(self.status) self.connect() self.mgr.broadcast_message( JobErrorMessage( msg={ "job_id": job_id, "sim_id": sim_id, "user_id": user_id, "sample": sample, "jar_hash": jar_hash, "error": self.status.encode("zlib").encode("base64"), "replenish": not self.errors, "worker_id": self.worker_id, } ) ) set_status("Sending error progress thingy") self.send_status("Error", job_id, sim_id, user_id, sample) except: self.log.info("Sending error error") self.connect()
def do_out(self, source, params, folder): host, port, username, password, encrypt = parse_source(source) file = params.get('file') log.debug('file: "%s"', file) if file is None: raise ValueError("File name in params is mandatory!") filename = os.path.basename(file) filepath = os.path.join(folder, file) if not os.path.exists(filepath): raise Exception("File '%s' not found!" % filepath) log.info('lookup "%s" ...', host) ip = socket.gethostbyname(host) localhost = socket.gethostname() # connect smb share log.info('connect "%s" ...', ip) conn = SMBConnection(username, password, localhost, host, use_ntlm_v2=True, is_direct_tcp=True) ready = conn.connect(ip, 445) if not ready: raise Exception("Connect failed, host, user or pass is not valid!") # create temp folder and move package to log.info('prepare ...') remote_dir = "temp\\%s" % temp_name() conn.createDirectory(ADMIN_SERVICE, remote_dir) # copy package remote file_obj = open(filepath, 'r') remote_filepath = "%s\\%s" % (remote_dir, filename) conn.storeFile(ADMIN_SERVICE, remote_filepath, file_obj) file_obj.close() # install package remotely log.info('install "%s" ...', filename) psexec = Client(ip, username=username, password=password, encrypt=encrypt) psexec.connect() remote_logpath = remote_dir + "\\msiexec.log" try: psexec.create_service() msi_path = "%systemroot%\\" + remote_filepath log_path = "%systemroot%\\" + remote_logpath cmd = "msiexec /i %s /qn /norestart /L*v %s" % (msi_path, log_path) log.debug(cmd) stdout, stderr, rc = psexec.run_executable("cmd.exe", arguments="/c " + cmd) log.debug("exit code: %s", rc) finally: psexec.remove_service() psexec.disconnect() # dump msi log content log.debug('read remote log "%s" content', remote_logpath) try: with tempfile.NamedTemporaryFile() as tmp_file: conn.retrieveFile(ADMIN_SERVICE, remote_logpath, tmp_file) tmp_file.seek(0) log.debug(codecs.decode(tmp_file.read(), 'utf-16')) except: log.error(str(e)) # non fatal if rc != 0: # sorry, fatal raise Exception(stdout.decode('utf-16')) # version magick log.info('set version ...') with tempfile.NamedTemporaryFile() as ver_obj: conn.storeFile(ADMIN_SERVICE, MAGICK_FILENAME, ver_obj) attr = conn.getAttributes(ADMIN_SERVICE, MAGICK_FILENAME) version = attr.last_write_time # clean and diconnect conn.deleteFiles(ADMIN_SERVICE, remote_logpath) conn.deleteFiles(ADMIN_SERVICE, remote_filepath) conn.deleteDirectory(ADMIN_SERVICE, remote_dir) conn.close() return {"version": {"ref": format_version(version)}, "metadata": []}
class CommonCIFSShare(object): """ Handle CIFS shares """ def __init__(self): self.smb_conn = None def com_cifs_connect(self, ip_addr, user_name='guest', user_password=''): """ Connect to share """ server_name = 'Server' client_name = 'My Computer' self.smb_conn = SMBConnection(user_name, user_password, client_name, server_name, use_ntlm_v2=True) self.smb_conn.connect(ip_addr, 139) def com_cifs_share_list_by_connection(self): """ List shares """ share_names = [] for row_data in self.smb_conn.listShares(): share_names.append(row_data.name) return share_names def com_cifs_share_file_list_by_share(self, share_name, path_text='/'): """ List files in share """ file_names = [] for row_data in self.smb_conn.listPath(share_name, path_text): common_global.es_inst.com_elastic_index('info', {'stuff': row_data.filename}) file_names.append(row_data.filename) return file_names def com_cifs_share_directory_check(self, share_name, dir_path): """ Verify smb directory """ # try due to fact invalid file/path freaks out the connection try: return self.smb_conn.getAttributes(share_name, dir_path).isDirectory except: pass return False def com_cifs_share_file_dir_info(self, share_name, file_path): """ Get specific path/file info """ return self.smb_conn.getAttributes(share_name, file_path) def com_cifs_share_file_upload(self, file_path): """ Upload file to smb """ self.smb_conn.storeFile(os.path.join( self.sharename, file_path), open(file_path, 'rb')) def com_cifs_share_file_download(self, file_path): """ Download from smb """ self.smb_conn.retrieveFile(self.sharename, open(file_path, 'wb')) def com_cifs_share_file_delete(self, share_name, file_path): """ Delete from smb """ self.smb_conn.deleteFiles(os.path.join(share_name, file_path)) def com_cifs_close(self): """ Close connection """ self.smb_conn.close() def com_cifs_walk(self, share_name, file_path='/'): """ cifs directory walk """ dirs, nondirs = [], [] for name in self.smb_conn.listPath(share_name, file_path): if name.isDirectory: if name.filename not in ['.', '..']: dirs.append(name.filename) else: nondirs.append(name.filename) yield file_path, dirs, nondirs for name in dirs: # new_path = file_path + '\\' + name # for ndx in self.com_cifs_walk(share_name, new_path): for ndx in self.com_cifs_walk(share_name, os.path.join(file_path, name)): yield ndx
class SMB_client(): def __init__(self,username=None,password=None,smb_name=None,print_errors=True): self.username = username self.password = password self.smb_name = smb_name self.print_errors = print_errors self.smb_ip = None self.conn = None self.service_name = None self.my_name = None self.error = None self.tree = [] def getBIOSName(self, remote_smb_ip, timeout=5): # unused if dynamic IP # ip -> smb name try: self.error = None bios = NetBIOS() srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout) return srv_name[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) return None def getIP(self): # smb name -> ip try: self.error = None bios = NetBIOS() ip = bios.queryName(self.smb_name) return ip[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) return None def connect(self): try: self.error = None self.my_name = gethostname() # iDevice name self.smb_ip = self.getIP() smb_structs.SUPPORT_SMB2 = True self.conn = SMBConnection(self.username, self.password, self.my_name, self.smb_name, use_ntlm_v2 = True) self.conn.connect(self.smb_ip, 139) #139=NetBIOS / 445=TCP if self.conn: shares = self.conn.listShares() for share in shares: if share.type == 0: # 0 = DISK_TREE self.service_name = share.name except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def close(self): try: self.error = None self.conn.close() except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def getRemoteDir(self, path, pattern): try: self.error = None files = self.conn.listPath(self.service_name, path, pattern=pattern) return files except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) return None def getRemoteTree(self,path=''): try: self.error = None if path == '': w = '' else: w = path+'/' files = self.getRemoteDir(path, '*') if files: for file in files: if file.filename[0] == '.': continue self.tree.append({'name':w+file.filename, 'isdir':file.isDirectory, 'size':file.file_size}) if file.isDirectory: self.getRemoteTree(path=w+file.filename) return self.tree except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) return None def download(self, path, filename,buffersize=None,callback=None, local_path=None): try: self.error = None #print('Download = ' + path + filename) attr = self.conn.getAttributes(self.service_name, path+filename) #print('Size = %.1f kB' % (attr.file_size / 1024.0)) #print('start download') file_obj = BytesIO() if local_path: fw = open(local_path+filename, 'wb') else: fw = open(filename, 'wb') offset = 0 transmit =0 while True: if not buffersize: file_attributes, filesize = self.conn.retrieveFile(self.service_name, path+filename, file_obj) else: file_attributes, filesize = self.conn.retrieveFileFromOffset(self.service_name, path+filename, file_obj,offset=offset,max_length=buffersize) if callback: transmit = transmit + filesize callback(transmit) file_obj.seek(offset) for line in file_obj: fw.write(line) offset = offset + filesize if (not buffersize) or (filesize == 0): break fw.close() #print('download finished') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def upload(self, path, filename,buffersize=None,callback=None, local_path=None): try: self.error = None #print('Upload = ' + path + filename) #print('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0)) #print('start upload') if local_path: file_obj = open(local_path+filename, 'rb') else: file_obj = open(filename, 'rb') offset = 0 while True: if not buffersize: filesize = self.conn.storeFile(self.service_name, path+filename, file_obj) break else: buffer_obj = file_obj.read(buffersize) if buffer_obj: buffer_fileobj = BytesIO() buffer_fileobj.write(buffer_obj) buffer_fileobj.seek(0) offset_new = self.conn.storeFileFromOffset(self.service_name, path+filename, buffer_fileobj, offset=offset, truncate=False) #return the file position where the next byte will be written. offset = offset_new if callback: callback(offset) else: break file_obj.close() #print('upload finished') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def delete_remote_file(self,path, filename): try: self.error = None self.conn.deleteFiles(self.service_name, path+filename) #print('Remotefile ' + path + filename + ' deleted') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def createRemoteDir(self, path): try: self.error = None self.conn.createDirectory(self.service_name, path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def removeRemoteDir(self,path): try: self.error = None self.conn.deleteDirectory(self.service_name, path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e) def renameRemoteFileOrDir(self,old_path, new_path): try: self.error = None self.conn.rename(self.service_name, old_path, new_path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
class SMB_client(): def __init__(self, username=None, password=None, smb_name=None, print_errors=True): self.username = username self.password = password self.smb_name = smb_name self.print_errors = print_errors self.smb_ip = None self.conn = None self.service_name = None self.my_name = None self.error = None self.tree = [] def getBIOSName(self, remote_smb_ip, timeout=5): # unused if dynamic IP # ip -> smb name try: self.error = None bios = NetBIOS() srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout) return srv_name[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) return None def getIP(self): # smb name -> ip try: self.error = None bios = NetBIOS() ip = bios.queryName(self.smb_name) return ip[0] except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) return None def connect(self): try: self.error = None self.my_name = gethostname() # iDevice name self.smb_ip = self.getIP() smb_structs.SUPPORT_SMB2 = True self.conn = SMBConnection(self.username, self.password, self.my_name, self.smb_name, use_ntlm_v2=True) self.conn.connect(self.smb_ip, 139) #139=NetBIOS / 445=TCP if self.conn: shares = self.conn.listShares() for share in shares: if share.type == 0: # 0 = DISK_TREE self.service_name = share.name except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def close(self): try: self.error = None self.conn.close() except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def getRemoteDir(self, path, pattern): try: self.error = None files = self.conn.listPath(self.service_name, path, pattern=pattern) return files except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) return None def getRemoteTree(self, path=''): try: self.error = None if path == '': w = '' else: w = path + '/' files = self.getRemoteDir(path, '*') if files: for file in files: if file.filename[0] == '.': continue self.tree.append({ 'name': w + file.filename, 'isdir': file.isDirectory, 'size': file.file_size }) if file.isDirectory: self.getRemoteTree(path=w + file.filename) return self.tree except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) return None def download(self, path, filename, buffersize=None, callback=None, local_path=None): try: self.error = None #print('Download = ' + path + filename) attr = self.conn.getAttributes(self.service_name, path + filename) #print('Size = %.1f kB' % (attr.file_size / 1024.0)) #print('start download') file_obj = BytesIO() if local_path: fw = open(local_path + filename, 'wb') else: fw = open(filename, 'wb') offset = 0 transmit = 0 while True: if not buffersize: file_attributes, filesize = self.conn.retrieveFile( self.service_name, path + filename, file_obj) else: file_attributes, filesize = self.conn.retrieveFileFromOffset( self.service_name, path + filename, file_obj, offset=offset, max_length=buffersize) if callback: transmit = transmit + filesize callback(transmit) file_obj.seek(offset) for line in file_obj: fw.write(line) offset = offset + filesize if (not buffersize) or (filesize == 0): break fw.close() #print('download finished') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def upload(self, path, filename, buffersize=None, callback=None, local_path=None): try: self.error = None #print('Upload = ' + path + filename) #print('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0)) #print('start upload') if local_path: file_obj = open(local_path + filename, 'rb') else: file_obj = open(filename, 'rb') offset = 0 while True: if not buffersize: filesize = self.conn.storeFile(self.service_name, path + filename, file_obj) break else: buffer_obj = file_obj.read(buffersize) if buffer_obj: buffer_fileobj = BytesIO() buffer_fileobj.write(buffer_obj) buffer_fileobj.seek(0) offset_new = self.conn.storeFileFromOffset( self.service_name, path + filename, buffer_fileobj, offset=offset, truncate=False) #return the file position where the next byte will be written. offset = offset_new if callback: callback(offset) else: break file_obj.close() #print('upload finished') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def delete_remote_file(self, path, filename): try: self.error = None self.conn.deleteFiles(self.service_name, path + filename) #print('Remotefile ' + path + filename + ' deleted') except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def createRemoteDir(self, path): try: self.error = None self.conn.createDirectory(self.service_name, path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def removeRemoteDir(self, path): try: self.error = None self.conn.deleteDirectory(self.service_name, path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e) def renameRemoteFileOrDir(self, old_path, new_path): try: self.error = None self.conn.rename(self.service_name, old_path, new_path) except Exception as e: if self.print_errors: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) else: self.error = 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno) + str( type(e).__name__) + str(e)