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 move( connection: SMBConnection, share_folder: str, file_path: str, input_file_path: str, temp_file_suffix=".tmp", ): logger.info( f"Moving {input_file_path} file to \\\\{connection.remote_name}\\{share_folder}{file_path}..." ) try: with open(input_file_path, "rb") as input_file: connection.storeFile(share_folder, f"{file_path}{temp_file_suffix}", input_file) except OperationFailure: raise Exception( f"Unable to write \\\\{connection.remote_name}\\{share_folder}{file_path}{temp_file_suffix}" ) if temp_file_suffix: try: connection.rename(share_folder, f"{file_path}{temp_file_suffix}", file_path) except OperationFailure: raise Exception( f"Unable to rename temp file into \\\\{connection.remote_name}\\{share_folder}{file_path}" ) logger.info(f"File copied. Removing {input_file_path} file...") os.remove(input_file_path) logger.info( f"{input_file_path} file moved within \\\\{connection.remote_name}\\{share_folder}{file_path}." )
class smb_connector: def __init__(self, ip, shared_directory, username, password): self.connection = SMBConnection(username, password, "this_machine", "remote_machine", use_ntlm_v2=True, is_direct_tcp=True) self.connection.connect(ip, 445) self.shared_directory = shared_directory def __enter__(self): return self def __exit__(self, type, value, traceback): self.close() def close(self): self.connection.close() def file_contents(self, path): with tempfile.NamedTemporaryFile() as file_obj: self.connection.retrieveFile(self.shared_directory, path, file_obj) file_obj.seek(0) content = file_obj.read().decode('utf-8', 'ignore').translate({ord('\u0000'): None}) return content def all_files_recursively(self, full_path, file_filter, directory_filter, relative_path=''): whats_here = self.connection.listPath(self.shared_directory, full_path) for file in whats_here: file_path = os.path.join(full_path, file.filename) file_relative_path = os.path.join(relative_path, file.filename) if file.isDirectory: if directory_filter(file.filename) and '.' not in file.filename: yield from self.all_files_recursively(file_path, file_filter, directory_filter, file_relative_path) elif file_filter(file.filename): yield os.path.normpath(file_relative_path) def write_file(self, path, contents): with tempfile.NamedTemporaryFile() as file_obj: bytes = file_obj.write(contents.encode('utf-8')) file_obj.seek(0) bytes = self.connection.storeFile(self.shared_directory, path, file_obj)
def uploadFile(self): filetypes = [("All Files", '*'), ("Python Files", '*.py', 'TEXT'), ("Text Files", '*.txt', 'TEXT'), ("Exe Files", '*.exe', 'TEXT')] fobj = filedialog.askopenfile(filetypes=filetypes) if fobj: self.upload_path = fobj.name a = len(self.upload_path.split('/')) try: conn = SMBConnection(self.username.get(), self.password.get(), self.my_name, self.domain_name, use_ntlm_v2=True, is_direct_tcp=True) conn.connect(self.remote_smb_IP.get(), int(self.port.get())) file_obj = open(self.upload_path, 'rb') conn.storeFile( self.dir, self.display_path + '/' + self.upload_path.split('/')[a - 1], file_obj) file_obj.close() return True except: return False else: pass
def put_file_smb(_filename): try: # create samba connection conn = SMBConnection(config.smb_username, config.smb_password, config.smb_localname, config.smb_remotename, '', use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=True) connected = conn.connect(config.smb_remotename, 445) # save file to share location try: with open(_filename, 'rb') as fstore: conn.storeFile(config.smb_sharename, config.smb_filename, fstore) #Response = conn.listShares(timeout=30) # obtain a list of shares #print('Shares on: ' + config.smb_remotename) #for i in range(len(Response)): # iterate through the list of shares # print(" Share[",i,"] =", Response[i].name) except Exception as e: logger.exception("Error storing file on remote share. " + str(e)) except Exception as e: logger.exception("Error establinshing samba connection. " + str(e)) finally: conn.close()
class CSmb: def __init__(self): pass def __exit__(self): pass def fileSave(self, remote_path, local_file): try: self.conn = SMBConnection('ywkim', 'gksQldi1!', 'dev', 'nas5', domain='', use_ntlm_v2=True) self.conn.connect('172.16.3.36', 445) # self.conn = SMBConnection(USER_NAME, PASSWORD, 'dev', 'nas5', domain='', use_ntlm_v2=True) # self.conn.connect(HOST_IP, PORT) except: print("[SMB]: Can not connect to NAS Server") else: try: f = open(local_file, 'rb') f.seek(0) self.conn.storeFile(remote_path, FILE_NAME, f) except: print("[SMB]: Can not transfer file to NAS Server") try: self.conn.close() except: print("[SMB]: Can not close connection")
class Handler: def __init__(self, system_name, user, domain, password, service_name, path=None): self.conn = SMBConnection(user, password, 'raspberry', system_name, domain) assert self.conn.connect(system_name) self.service_name = service_name self.path = path if path else '/' def upload_file(self, local_path, samba_path=''): file_name = os.path.basename(samba_path) if not file_name: # if no filename it copies the local filename file_name = os.path.basename(local_path) samba_path = os.path.join(samba_path, file_name) samba_path = os.path.join(self.path, samba_path) with open(local_path, 'rb') as f: print(samba_path) self.conn.storeFile(self.service_name, samba_path, f) def download_file(self, samba_path, local_path=None): raise NotImplementedError def get_shared_link(self, samba_path): raise NotImplementedError
class SambaHelper(): def __init__(self, user, password, serverName): self.__username = user self.__password = password self.__connect(serverName) def __connect(self, serverName): self.conn = SMBConnection(self.__username, self.__password, '','',use_ntlm_v2 = True) self.conn.connect(serverName, 139) print "Connected." def CopyFileToSambaShare(self, fileName, shareName): file_obj=file(fileName, 'r') print file_obj.name self.conn.storeFile(shareName, '/{0}'.format(fileName), file_obj) def CopyFilesToSambaShare(self, inputDir, shareName): files = os.listdir(inputDir) for file in files: if file.endswith('.jpg'): print file self.CopyFileTo("{0}/{1}".format(inputDir,file), shareName) def CloseConnection(): self.conn.close()
def samba_put(self, service_name, localfile, remotefile): samba = SMBConnection(self.username, self.password, '', '') samba.connect(self.ip, self.port) def list_put(share_name, localstuff, remotestuff): dirname = os.path.dirname(remotestuff) basename = os.path.basename(remotestuff) data = [] for value in samba.listPath(share_name, dirname): data.append(value.filename) else: if basename not in data: samba.createDirectory(share_name, remotestuff) for content in os.listdir(localstuff): localpath = localstuff + os.sep + content remotepath = remotestuff + '/' + content if os.path.isfile(localpath): handler = open(localpath, 'rb') samba.storeFile(share_name, remotepath, handler) handler.close() else: list_put(share_name, localpath, remotepath) try: if os.path.isfile(localfile): handle = open(localfile, 'rb') samba.storeFile(service_name, remotefile, handle) handle.close() else: list_put(service_name, localfile, remotefile) except Exception: print('upload file failure! ') finally: samba.close()
def storage_writer(): global running while running or q.qsize() != 0: log_file, data = q.get() trying = 5 while trying > 0: try: conn = SMBConnection(credentials['username'], credentials['password'], credentials['client_machine_name'], credentials['server_name'], use_ntlm_v2=True) assert conn.connect(settings['file_storage']['host'], settings['file_storage']['port']) second_file_obj = tempfile.NamedTemporaryFile() second_file_obj.seek(0) second_file_obj.write(data.read()) second_file_obj.seek(0) print('writing.....') conn.storeFile(settings['file_storage']['service_name'], log_file, second_file_obj) print('done writing.....') trying = 0 break except: print('failed') time.sleep(trying / 2) trying -= 1 print('{} writes left to do..', q.qsize()) conn.close() data.close() print('writer closed'.format(log_file))
def _push_pictures(self): """ Push the new picture into the NAS for GP. """ # Retrieve configuration smb_user = config.get('smb_user') smb_pass = config.get('smb_pwd') smb_ip = config.get('smb_ip') smb_port = int(config.get('smb_port', 0)) if not (smb_user and smb_pass and smb_ip and smb_port): raise Warning('Config Error', 'Missing Samba configuration in conf file.') child = self.child_id date_pic = self.date.replace('-', '') gp_pic_path = "{0}{1}/".format(config.get('gp_pictures'), child.unique_id[:2]) file_name = "{0}_{1}.jpg".format(child.unique_id, date_pic) picture_file = TemporaryFile() picture_file.write(base64.b64decode(self.fullshot)) picture_file.flush() picture_file.seek(0) # Upload file to shared folder smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas') if smb_conn.connect(smb_ip, smb_port): try: smb_conn.storeFile('GP', gp_pic_path + file_name, picture_file) except OperationFailure: # Directory may not exist smb_conn.createDirectory('GP', gp_pic_path) smb_conn.storeFile('GP', gp_pic_path + file_name, picture_file)
def store_smb_file(user,password,cliente_name,server_name,domain,ip_server,fpath_client): #init connection try: conn = SMBConnection(user, password, cliente_name, server_name, domain, use_ntlm_v2 = True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=False) print('Connected to %s' % server_name) assert conn.connect(ip_server,139) #Entry file Vxxxxxxxxxxx.txt/xml/csv #Description The content of the file, it must be a line with the amount multiplied by 100. #On the second line you have to indicate 0: Hide the main screen, 1 show the #main screen. #Example: #275. It manages a sale of 2,75€. f = open('V2.txt','w') f.truncate() f.write("275") f.write("\n") f.write("1") f = open('V2.txt','r+') conn.storeFile(fpath_client,"/"+f.name, f) f.close() except Exception, e: print('Connect failed. Reason: %s', e) return False
def save_file_to_smb(build_number, language): try: name = socket.gethostbyaddr(const.nas_ip) ipGet = socket.gethostbyname(name[0]) print(name, ipGet, sep='\n') remote_name = name[0] smb_conn = SMBConnection(const.nas_account, const.nas_password, 'any_name', remote_name) assert smb_conn.connect(const.nas_ip, timeout=30) prepack_path = const.HF_Working_Folder + '\\Build\\' + language f = open(prepack_path + '\\B' + build_number + '\\Prepack.zip', 'rb') if language == 'ja': language = 'JP' elif language == 'zh_CN': language = 'SC' smb_conn.storeFile( 'Backup_Build', '\\' + language + '\\Prepack_' + build_number + '.zip', f) smb_conn.close() f.close() print('Remove local file...') os.remove(prepack_path + '\\B' + build_number + '\\Prepack.zip') except Exception as e: # log(str(e), level.error) print(str(e)) raise Exception('Failed! Save file to NAS unsuccessfully.')
def store_smb_file(user, password, cliente_name, server_name, domain, ip_server, fpath_client): #init connection try: conn = SMBConnection(user, password, cliente_name, server_name, domain, use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=False) print('Connected to %s' % server_name) assert conn.connect(ip_server, 139) #Payment Automatic #Entry File: Pxxxxxxxxxxx.txt/xml/csv #Description The content of the file, it must be a line with the amount multilpied #by 100. #Example: #275. It generates us a payment of 2,75€. f = open('P2.txt', 'w') f.truncate() f.write("275") f.write("\n") f.write("1") f = open('P2.txt', 'r+') conn.storeFile(fpath_client, "/" + f.name, f) f.close() except Exception, e: print('Connect failed. Reason: %s', e) return False
def _transfer_file_on_nas(self, file_name): """ Puts the letter file on the NAS folder for the translation platform. :return: None """ self.ensure_one() # Retrieve configuration smb_user = config.get('smb_user') smb_pass = config.get('smb_pwd') smb_ip = config.get('smb_ip') smb_port = int(config.get('smb_port', 0)) if not (smb_user and smb_pass and smb_ip and smb_port): raise Exception('No config SMB in file .conf') # Copy file in the imported letter folder smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas') if smb_conn.connect(smb_ip, smb_port): file_ = BytesIO(base64.b64decode( self.letter_image.with_context( bin_size=False).datas)) nas_share_name = self.env.ref( 'sbc_translation.nas_share_name').value nas_letters_store_path = self.env.ref( 'sbc_translation.nas_letters_store_path').value + file_name smb_conn.storeFile(nas_share_name, nas_letters_store_path, file_) logger.info('File {} store on NAS with success' .format(self.letter_image.name)) else: raise Warning(_('Connection to NAS failed'))
def putSMB(task, file): try: samba = SMBConnection(task.username, task.password, 'mybase', task.target) samba.connect(task.dir, int(task.port)) list = task.tables.split(' ') share = list[0] #TESt path = list[1] #/ s = file.split('/')[1] l = len(s) + 1 + 5 t = file[l:] #temp/share/ ddddd if path[-1] == '/': path += t #/ddd else: path = path + '/' + t # 取路径 dir = os.path.split(path)[0] # 去除首位空格 dir = dir.strip() # 去除尾部 \ 符号 dir = dir.rstrip("\\") if dir != '/': mkdir(samba, share, dir) with open(file, 'rb') as f: samba.storeFile(share, path, f) return True except Exception as e: LOGR.error('发送SMB文件错误:' + str(e))
def _transfer_file_on_nas(self, file_name): """ Puts the letter file on the NAS folder for the translation platform. :return: None """ self.ensure_one() # Retrieve configuration smb_user = config.get('smb_user') smb_pass = config.get('smb_pwd') smb_ip = config.get('smb_ip') smb_port = int(config.get('smb_port', 0)) if not (smb_user and smb_pass and smb_ip and smb_port): raise Exception('No config SMB in file .conf') # Copy file in the imported letter folder smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas') if smb_conn.connect(smb_ip, smb_port): file_ = BytesIO(base64.b64decode(self.letter_image)) nas_share_name = self.env.ref( 'sbc_switzerland.nas_share_name').value nas_letters_store_path = self.env.ref( 'sbc_switzerland.nas_letters_store_path').value + file_name smb_conn.storeFile(nas_share_name, nas_letters_store_path, file_) logger.info('File {} store on NAS with success'.format( self.file_name)) else: raise UserError(_('Connection to NAS failed'))
def _transfer_file_on_nas(self, file_name): """ Puts the letter file on the NAS folder for the translation platform. :return: None """ self.ensure_one() # Retrieve configuration smb_user = config.get("smb_user") smb_pass = config.get("smb_pwd") smb_ip = config.get("smb_ip") smb_port = int(config.get("smb_port", 0)) if not (smb_user and smb_pass and smb_ip and smb_port): raise Exception("No config SMB in file .conf") # Copy file in the imported letter folder smb_conn = SMBConnection(smb_user, smb_pass, "openerp", "nas") if smb_conn.connect(smb_ip, smb_port): file_ = BytesIO(self.get_image()) nas_share_name = self.env.ref( "sbc_switzerland.nas_share_name").value nas_letters_store_path = ( self.env.ref("sbc_switzerland.nas_letters_store_path").value + file_name) smb_conn.storeFile(nas_share_name, nas_letters_store_path, file_) else: raise UserError(_("Connection to NAS failed"))
def notify_smb(isn, timestamp, msg): server_name = "" client_machine_name = "Onegin" server_ip = "172.18.212.211" userID = "szsfis" password = "******" conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True) assert conn.connect(server_ip, 139) filename = "%s_%s.csv" % (isn, timestamp) conn.storeFile("sz_sfis_event_log", filename, StringIO.StringIO(msg))
def upload_file_to_smb(self): conn = SMBConnection(self.login, self.password, socket.gethostname(), self.domain, use_ntlm_v2=True, is_direct_tcp=True) conn.connect(self.host, port=445) with open(self.outfilename, 'rb') as outfile: conn.storeFile(self.path, self.outfilename, outfile, timeout=300) conn.close()
def store_file_on_samba_server(file_name): """ file_name is a full file path""" 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) with open(file_name, 'rb') as f: conn.storeFile(SMB_SHARED_FOLDER, os.path.basename(file_name), f) return True except: False
def move( connection: SMBConnection, share_folder: str, file_path: str, input_file_path: str, temp_file_suffix=".tmp", timeout=30, write_to_new_folder_after=1, ): """ Move a local file to a Windows location. :param connection: Samba connection as returned by connect function. :param share_folder: Shared folder name. :param file_path: Expected full path to the file that should be created. Folders will be created if needed. :param input_file_path: Path to the file to move. :param temp_file_suffix: Suffix of the file while being copied. Default to ".tmp". :param timeout: Maximum amount of seconds to write the file. Default to 30 seconds. :param write_to_new_folder_after: Number of seconds to wait before writing file if folder needed to be created. Useful as Microsoft does not seems to release folder right away. Default to 1 second. """ logger.info( f"Moving {input_file_path} file to \\\\{connection.remote_name}\\{share_folder}{file_path}..." ) if _create_folders(connection, share_folder, os.path.dirname(file_path)): time.sleep(write_to_new_folder_after) try: with open(input_file_path, "rb") as input_file: connection.storeFile(share_folder, f"{file_path}{temp_file_suffix}", input_file, timeout) except OperationFailure: raise PyndowsException( f"Unable to write \\\\{connection.remote_name}\\{share_folder}{file_path}{temp_file_suffix}" ) if temp_file_suffix: try: connection.rename(share_folder, f"{file_path}{temp_file_suffix}", file_path) except OperationFailure: raise PyndowsException( f"Unable to rename temp file into \\\\{connection.remote_name}\\{share_folder}{file_path}" ) logger.info(f"File copied. Removing {input_file_path} file...") os.remove(input_file_path) logger.info( f"{input_file_path} file moved within \\\\{connection.remote_name}\\{share_folder}{file_path}." )
def upload_samba_file(self, filename, upload_path): try: conn = SMBConnection(self.user_name, self.password, self.my_name, self.domain_name, use_ntlm_v2=True) conn.connect(self.remote_smb_ip, self.port) file_fd = open(upload_path + filename, 'rb') conn.storeFile(self.dir, filename, file_fd) file_fd.close() print_info.print_info(print_info.PRINT_INFO, "samba upload file ok") return 1 except Exception, e : print_string = "samba upload file error %s" % e print_info.print_info(print_info.PRINT_ERROR, print_string) return 0
def notify_smb(isn, timestamp, msg): server_name = "" client_machine_name = "Onegin" server_ip = "172.18.212.211" userID = "szsfis" password = "******" conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2=True) assert conn.connect(server_ip, 139) filename = "%s_%s.csv" % (isn, timestamp) conn.storeFile("sz_sfis_event_log", filename, StringIO.StringIO(msg))
def uploadFilenormal(self,upload_filename,datetime_filename):#上传文件 self.root=os.getcwd() self.upload_path = self.root self.upload_filename=upload_filename #a = len(self.upload_path.split('/')) self.ctipath=os.path.join(datetime_filename,'cdrinsert.py') 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) file_obj = open(self.upload_path+'\\'+upload_filename, 'rb') #4.待上传的文件全路径 conn.storeFile(self.dir, self.ctipath, file_obj) #(1.共享文件夹名字, 2.上传文件的全路径包含文件名,3.待上传的文件全路径) file_obj.close() return True except: return False
def uploadFile(self, filename, upload_path): ''' 上传文件 :param filename: 上传文件的名称 :param upload_path: 上传文件的路径 :return:True or False ''' try: conn = SMBConnection(self.user_name, self.pass_word, self.my_name, self.domain_name, use_ntlm_v2=True) conn.connect(self.remote_smb_IP, self.port) file_obj = open(upload_path + filename, 'rb') conn.storeFile(self.dir, filename, file_obj) file_obj.close() return True except: return False
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 uploadCmdrWatch(barcodeFile, dataType, data, config): try: localslug = re.sub('[^\w-]+', '_', barcodeFile).strip().lower() barcodeFh = codecs.open('/tmp/%s' % localslug, 'w', 'utf-8-sig') csvlogfh = csv.writer(barcodeFh, delimiter=",", quoting=csv.QUOTE_ALL) if dataType == 'locationLabels': csvlogfh.writerow('termdisplayname'.split(',')) for d in data: csvlogfh.writerow((d[0], )) # writerow needs a tuple or array elif dataType == 'objectLabels': csvlogfh.writerow( 'MuseumNumber,ObjectName,PieceCount,FieldCollectionPlace,AssociatedCulture,EthnographicFileCode' .split(',')) for d in data: csvlogfh.writerow(d[3:9]) barcodeFh.close() except: # raise barcodeFile = '<span style="color:red;">could not write to /tmp/%s</span>' % localslug return barcodeFile try: # OK, now we have the file object with the data in it. write it to the # commanderWatch server via SMB domain = config.get('files', 'domain') userID = config.get('files', 'userID') password = config.get('files', 'password') client_name = config.get('files', 'client_name') server_ip = config.get('files', 'server_ip') service_name = config.get('files', 'service_name') # client_machine_name can be an arbitary ASCII string # server_name should match the remote machine name, or else the connection will be rejected # # SMBConnection(username, password, my_name, remote_name, domain='') conn = SMBConnection(userID, password, client_name, service_name, domain, is_direct_tcp=True) assert conn.connect(server_ip, 445) # storeFile(service_name, path, file_obj, timeout=30) # service_name - the name of the shared folder for the path barcodeFh = open('/tmp/%s' % localslug, 'rb') bytes = conn.storeFile(service_name, barcodeFile, barcodeFh) barcodeFh.close() os.unlink('/tmp/%s' % localslug) return barcodeFile except: # raise os.unlink('/tmp/%s' % localslug) barcodeFile = '<span style="color:red;">could not transmit %s to commanderWatch</span>' % barcodeFile return barcodeFile
class SCP: def __init__(self, user, pwd, client, server): self.conn = SMBConnection(user, pwd, client, server, use_ntlm_v2=True) self.conn.connect(server, 139) def get_file(self, share, folder, filename): remote_file = os.path.join(folder, filename) local_file = open(filename, "wb") self.conn.retrieveFile(share, remote_file, local_file) print("Retrieved {}".format(remote_file)) def send_file(self, share, folder, filename): local_file = os.path.join(folder, filename) remote_file = open(filename, "rb") self.conn.storeFile(share, local_file, remote_file) print("Uploaded {}".format(local_file))
def test_install(vm): s = winrm.Session('localhost:5995', ('vagrant', 'vagrant'), read_timeout_sec=180, operation_timeout_sec=120) logging.info("uploading installer.msi") c = SMBConnection('vagrant', 'vagrant', 'PYTHON', 'vagrant-10', is_direct_tcp=True) c.connect('127.0.0.1', port=8445) with open('installer/installer.msi', 'rb') as f: c.storeFile('Desktop', 'installer.msi', f) c.close() logging.info("uploaded installer msi") s.run_cmd('start', ['/wait', r'msiexec.exe', '/i', r'C:\Users\vagrant\Desktop\installer.msi', '/qn', '/l*v', r'C:\msi.log']) logging.info("check if appric folder is installed") c = SMBConnection('vagrant', 'vagrant', 'PYTHON', 'vagrant-10', is_direct_tcp=True) c.connect('127.0.0.1', port=8445) c.listPath('C$', r'Program Files (x86)\Mobivity\AppRIC') c.close() logging.info("AppRIC is installed!")
def uploadFile(current_user, service_name, path, file): userID = current_user.name password = current_user.password try: conn = SMBConnection(userID, password, cifs_host_name, cifs_server_name, use_ntlm_v2=True) conn.connect(cifs_server_ip, 139) conn.storeFile(service_name, path, file) except Exception as e: exception_type, exception_object, exception_traceback = sys.exc_info() filename = exception_traceback.tb_frame.f_code.co_filename line_number = exception_traceback.tb_lineno print("Exception type: ", exception_type, "\n File name: ", filename, "\n Line number: ", line_number) print("Exception: ", e)
def handle_uploaded_file(request, file, filename): global PASSCODE user_name = request.user.username filename = str(filename) ## Creating A samba connection for file to be uploaded to samba server client_machine_name = 'megh-os-101317.megh.barc.gov.in' server_name = 'megh-os-101317.megh.barc.gov.in' userID = request.session['username'] password = PASSCODE conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2=True) server_ip = '10.35.28.189' assert conn.connect(server_ip, 139) # Connection Established # file obj in which content will be read in chunks file_obj = tempfile.NamedTemporaryFile() for chunk in file.chunks(): file_obj.write(chunk) file_obj.seek(0) # path to store that file in user area storingpath = '/' + filename # Actually storing file conn.storeFile('homes', storingpath, file_obj) file_obj.close() # Closing the connection conn.close() #abs_path = os.path.abspath(path) # abs path example - /home/u1/file.txt for 'file.txt' to be uploaded # This is useful in creating file record in database to know where files are actually in Samba Server abs_path = '/home/' + userID + '/' + filename print(abs_path) return filename, abs_path
def uploadCmdrWatch(barcodeFile, dataType, data, config): try: localslug = re.sub('[^\w-]+', '_', barcodeFile).strip().lower() barcodeFh = codecs.open('/tmp/%s' % localslug, 'w', 'utf-8-sig') csvlogfh = csv.writer(barcodeFh, delimiter=",", quoting=csv.QUOTE_ALL) if dataType == 'locationLabels': csvlogfh.writerow('termdisplayname'.split(',')) for d in data: csvlogfh.writerow((d[0],)) # writerow needs a tuple or array elif dataType == 'objectLabels': csvlogfh.writerow( 'MuseumNumber,ObjectName,PieceCount,FieldCollectionPlace,AssociatedCulture,EthnographicFileCode'.split( ',')) for d in data: csvlogfh.writerow(d[3:9]) barcodeFh.close() except: # raise barcodeFile = '<span style="color:red;">could not write to /tmp/%s</span>' % localslug return barcodeFile try: # OK, now we have the file object with the data in it. write it to the # commanderWatch server via SMB domain = config.get('files', 'domain') userID = config.get('files', 'userID') password = config.get('files', 'password') client_name = config.get('files', 'client_name') server_ip = config.get('files', 'server_ip') service_name = config.get('files', 'service_name') # client_machine_name can be an arbitary ASCII string # server_name should match the remote machine name, or else the connection will be rejected # # SMBConnection(username, password, my_name, remote_name, domain='') conn = SMBConnection(userID, password, client_name, service_name, domain, is_direct_tcp=True) assert conn.connect(server_ip, 445) # storeFile(service_name, path, file_obj, timeout=30) # service_name - the name of the shared folder for the path barcodeFh = open('/tmp/%s' % localslug, 'rb') bytes = conn.storeFile(service_name, barcodeFile, barcodeFh) barcodeFh.close() os.unlink('/tmp/%s' % localslug) return barcodeFile except: # raise os.unlink('/tmp/%s' % localslug) barcodeFile = '<span style="color:red;">could not transmit %s to commanderWatch</span>' % barcodeFile return barcodeFile
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 upload_file_by_smb(self, host, host_name, username, password, shared_folder_name, remote_file_path, local_file_path): """ upload file by SMB """ try: remote_name = host_name or self.get_BIOSName(host) conn = SMBConnection(username, password, '', remote_name, use_ntlm_v2=True) assert conn.connect(host) with open(local_file_path, 'rb') as f: conn.storeFile(shared_folder_name, remote_file_path, f) except Exception as e: logger.error('SMB file download error. %s' % str(e)) finally: conn.close()
def uploadFilewithFD(self):#上传文件使用filedialog filetypes = [ ("All Files", '*'), ("Python Files", '*.py', 'TEXT'), ("Text Files", '*.txt', 'TEXT'), ("Exe Files", '*.exe', 'TEXT')] #列表指定选择指定格式的文件 fobj = filedialog.askopenfile(filetypes=filetypes) #调用tkinter的filedialog打开文件 if fobj: self.upload_path = fobj.name a = len(self.upload_path.split('/')) 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) file_obj = open(self.upload_path, 'rb') conn.storeFile(self.dir, self.display_path + self.upload_path.split('/')[a - 1], file_obj) file_obj.close() return True except: return False else: pass
def attach_pictures(self, cr, uid, ids, pictures_id, context=None): """ Push the new picture. """ res = super(child_property, self).attach_pictures( cr, uid, ids, pictures_id, context) pictures = self.pool.get('compassion.child.pictures').browse( cr, uid, pictures_id, context) # Retrieve configuration smb_user = config.get('smb_user') smb_pass = config.get('smb_pwd') smb_ip = config.get('smb_ip') smb_port = int(config.get('smb_port', 0)) if not (smb_user and smb_pass and smb_ip and smb_port): raise orm.except_orm( 'Config Error', 'Missing Samba configuration in conf file.') child = pictures.child_id date_pic = pictures.date.replace('-', '') gp_pic_path = "{0}{1}/".format(config.get('gp_pictures'), child.code[:2]) file_name = "{0}_{1}.jpg".format(child.code, date_pic) picture_file = TemporaryFile() picture_file.write(base64.b64decode(pictures.fullshot)) picture_file.flush() picture_file.seek(0) # Upload file to shared folder smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas') if smb_conn.connect(smb_ip, smb_port): try: smb_conn.storeFile( 'GP', gp_pic_path + file_name, picture_file) except OperationFailure: # Directory may not exist smb_conn.createDirectory('GP', gp_pic_path) smb_conn.storeFile( 'GP', gp_pic_path + file_name, picture_file) return res
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 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)
def smb_open(self, req): global USE_NTLM, MACHINE_NAME host = req.get_host() if not host: raise urllib.error.URLError('SMB error: no host given') host, port = splitport(host) if port is None: port = 139 else: port = int(port) # username/password handling user, host = splituser(host) if user: user, passwd = splitpasswd(user) else: passwd = None host = unquote(host) user = user or '' passwd = passwd or '' myname = MACHINE_NAME or self.generateClientMachineName() n = NetBIOS() names = n.queryIPForName(host) if names: server_name = names[0] else: raise urllib.error.URLError('SMB error: Hostname does not reply back with its machine name') path, attrs = splitattr(req.get_selector()) if path.startswith('/'): path = path[1:] dirs = path.split('/') dirs = list(map(unquote, dirs)) service, path = dirs[0], '/'.join(dirs[1:]) try: conn = SMBConnection(user, passwd, myname, server_name, use_ntlm_v2 = USE_NTLM) conn.connect(host, port) headers = email.message.Message() if req.has_data(): data_fp = req.get_data() filelen = conn.storeFile(service, path, data_fp) headers.add_header('Content-length', '0') fp = BytesIO(b"") else: fp = self.createTempFile() file_attrs, retrlen = conn.retrieveFile(service, path, fp) fp.seek(0) mtype = mimetypes.guess_type(req.get_full_url())[0] if mtype: headers.add_header('Content-type', mtype) if retrlen is not None and retrlen >= 0: headers.add_header('Content-length', '%d' % retrlen) return addinfourl(fp, headers, req.get_full_url()) except Exception as ex: raise urllib.error.URLError('smb error: %s' % ex).with_traceback(sys.exc_info()[2])
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()
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
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 smb_open(self, req): global USE_NTLM, MACHINE_NAME host = req.get_host() if not host: raise urllib2.URLError('SMB error: no host given') host, port = splitport(host) if port is None: port = 139 else: port = int(port) # username/password handling user, host = splituser(host) if user: user, passwd = splitpasswd(user) else: passwd = None host = unquote(host) user = user or '' domain = '' if ';' in user: domain, user = user.split(';', 1) passwd = passwd or '' myname = MACHINE_NAME or self.generateClientMachineName() n = NetBIOS() names = n.queryIPForName(host) if names: server_name = names[0] else: raise urllib2.URLError('SMB error: Hostname does not reply back with its machine name') path, attrs = splitattr(req.get_selector()) if path.startswith('/'): path = path[1:] dirs = path.split('/') dirs = map(unquote, dirs) service, path = dirs[0], '/'.join(dirs[1:]) try: conn = SMBConnection(user, passwd, myname, server_name, domain=domain, use_ntlm_v2 = USE_NTLM) conn.connect(host, port) if req.has_data(): data_fp = req.get_data() filelen = conn.storeFile(service, path, data_fp) headers = "Content-length: 0\n" fp = StringIO("") else: fp = self.createTempFile() file_attrs, retrlen = conn.retrieveFile(service, path, fp) fp.seek(0) headers = "" mtype = mimetypes.guess_type(req.get_full_url())[0] if mtype: headers += "Content-type: %s\n" % mtype if retrlen is not None and retrlen >= 0: headers += "Content-length: %d\n" % retrlen sf = StringIO(headers) headers = mimetools.Message(sf) return addinfourl(fp, headers, req.get_full_url()) except Exception, ex: raise urllib2.URLError, ('smb error: %s' % ex), sys.exc_info()[2]