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)
Esempio n. 2
0
    def smb(self):
        userID = self.args.user[0]
        password = '******'
        client_machine_name = 'pentesting'
        server_name = 'winxpSP3'
        domain_name = 'localhost'
        server_ip = self.args.ip
        conn = SMBConnection(userID,
                             password,
                             client_machine_name,
                             server_name,
                             domain=domain_name,
                             use_ntlm_v2=True,
                             is_direct_tcp=True)
        conn.connect(server_ip, 445)
        shares = conn.listShares()

        for share in shares:
            if not share.isSpecial and share.name not in [
                    'NETLOGON', 'SYSVOL'
            ]:
                sharedfiles = conn.listPath(share.name, '/')
                for sharedfile in sharedfiles:
                    print(sharedfile.filename)

        conn.close()
        print("You are now in the smb method")
Esempio n. 3
0
def smb_scan(ip, port, list_shares, timeout, verbose):
	# empty username and password for null session
	username = ""
	password = ""
	client_name = "client"
	server_name = ip
	if port == 445:
		is_direct_tcp = True
	else:
		is_direct_tcp = False
	try:
		# def __init__(self, username, password, my_name, remote_name, domain = '', use_ntlm_v2 = True, sign_options = SIGN_WHEN_REQUIRED, is_direct_tcp = False)
		conn = SMBConnection(username, password, client_name, server_name, use_ntlm_v2 = True, is_direct_tcp = is_direct_tcp)
		smb_authentication_successful = conn.connect(ip, port, timeout = timeout)
		if smb_authentication_successful:
			print "SMB active [null session enabled]: %s:%s" % (ip, port)
			if list_shares:
				list_smb_shares(conn, timeout)
		else:
			# on Windows 7 authentication fails due to disabled null sessions
			print "SMB active [null session disabled]: %s:%s" % (ip, port)
	except:
		if verbose:
			e = sys.exc_info()
			print "%s" % str(e)
	finally:
		if conn:
			conn.close()
Esempio n. 4
0
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")
Esempio n. 5
0
 def run(self):
     print "Starting thread for " + self.ip
     net = NetBIOS()
     net_name = str(net.queryIPForName(self.ip)).strip("['").strip("']")
     net.close()
     conn = SMBConnection(self.user,
                          self.pwd,
                          'cobwebs',
                          net_name,
                          domain=self.domain,
                          use_ntlm_v2=False)
     if conn.connect(self.ip, port=139, timeout=10):
         print(
             "Connecting to %s was successful! How about a nice game of spidering %s%s?"
             % (self.ip, self.share, self.subfolder))
     else:
         print("Connection error: %s" % (self.ip))
     if self.recursive > 0:
         recurse(conn, self.ip, self.share, self.subfolder, self.pattern,
                 int(self.recursive))
     else:
         filelist = conn.listPath(self.share, self.subfolder)
         dir_list(filelist, self.ip, self.subfolder, self.pattern)
     conn.close()
     print "Exiting thread for " + self.ip
Esempio n. 6
0
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.')
Esempio n. 7
0
    def poll(self, poll_input):
        socket.setdefaulttimeout(poll_input.timeout)

        username = poll_input.credentials.username
        password = poll_input.credentials.password
        domain = poll_input.credentials.domain

        try:
            #            n = NetBIOS()
            #            hostname = n.queryIPForName(poll_input.server,timeout=10)[0]
            #            n.close()
            if domain is None:
                conn = SMBConnection(username, password, '',
                                     poll_input.hostname)
            else:
                conn = SMBConnection(username, password, '',
                                     poll_input.hostname, domain.domain)

            conn.connect(poll_input.server,
                         poll_input.port,
                         timeout=poll_input.timeout)
            t = tempfile.TemporaryFile()
            conn.retrieveFile(poll_input.sharename, poll_input.path, t)
            conn.close()

            t.seek(0)
            result = SmbPollResult(t.read())
            return result
        except Exception as e:
            result = SmbPollResult(None, e)
            return result
Esempio n. 8
0
def test_smb2x():
    smb_structs.SUPPORT_SMB2 = smb_structs.SUPPORT_SMB2x = False
    info = getConnectionInfo()
    conn = SMBConnection(info['user'], info['password'], info['client_name'], info['server_name'], use_ntlm_v2 = True, smb_support_mask = SMBConnection.SMB_SUPPORT_SMB2 | SMBConnection.SMB_SUPPORT_SMB2x)
    assert conn.connect(info['server_ip'], info['server_port'])
    assert conn.smb2_dialect == SMB2_DIALECT_21
    conn.close()
Esempio n. 9
0
def connect(username: str, password: str, remote_name: str,
            ip_address: str) -> Generator[SMBConnection, None, None]:
    logging.debug("Creating SMB connection")
    conn = SMBConnection(username,
                         password,
                         socket.gethostname(),
                         remote_name,
                         is_direct_tcp=True)

    try:
        logging.debug("Connecting to %s on port 445", ip_address)
        success = conn.connect(ip_address, 445)
    except (OSError, NotConnectedError):
        raise RuntimeError(
            "Could not connect to the remote host. Check your ip address and remote name."
        )

    if not success:
        raise RuntimeError(
            "Connection to the remote host was declined. Check your credentials."
        )

    logging.debug("Connection successfully established")

    try:
        yield conn
    finally:
        conn.close()
Esempio n. 10
0
    def action_verifica_connessione(self, cr, uid, ids, context=None):
        esito_test = True

        rec = self.browse(cr, uid, ids[0], context=context)

        if rec.import_attivo:
            esito_test = True
            conn = None

            user = rec.auth and rec.user or ''
            password = rec.auth and rec.password or ''
            local_name = rec.title.encode('ascii', 'ignore')
            remote_name = rec.address.encode('ascii', 'ignore')
            port = rec.port

            try:
                conn = SMBConnection(user, password, local_name, remote_name)
                esito_test = conn.connect(remote_name, port)
                self._directory_check(conn, rec.share, os.sep,
                                      rec.processed_folder)

            except Exception as exception:
                esito_test = False

            finally:
                if esito_test:
                    self.write(cr, uid, ids[0],
                               {'test_connessione': 'Connesso'}, context)
                    conn.close()
                else:
                    self.write(cr, uid, ids[0],
                               {'test_connessione': 'Fallito'}, context)
                    conn.close()
Esempio n. 11
0
def ListaRecursosServidor(userID, password, local_ip, server_ip, domain_name):
    # Devuelvo una lista en formato UTF-8 con los recursos disponibles via samba
    # del servidor especificado con server_ip, con el nombre del dominio en domain_name
    # y como autenticación el usuario userID y la contraseña password
    # Si no conseguimos autenticarnos con el sistema remoto imprimos el error y damos una lista vacia
    server_name = getBIOSName(server_ip, timeout=5)
    client_machine_name = getBIOSName(local_ip, timeout=5)
    #print domain_name
    Aux = []
    if (server_name == 'ERROR'):
        print 'No somos capaces de saber el nombre remoto por NETBIOS usamos su direccion Ip: ' + server_ip
        server_name = server_ip
    try:
        #print userID, password, client_machine_name, server_name, domain_name
        conn = SMBConnection(userID,
                             password,
                             client_machine_name,
                             server_name,
                             domain=domain_name,
                             use_ntlm_v2=True,
                             is_direct_tcp=True)
        conn.connect(server_ip, 445)
        shares = conn.listShares()
        for share in shares:
            if not share.isSpecial and share.name not in [
                    'NETLOGON', 'SYSVOL', 'REPL$'
            ]:
                Aux.append(share.name)
        conn.close()
    except:
        print 'Error con el usuario: ' + userID + ' y la maquina: ' + server_ip
    return Aux
Esempio n. 12
0
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()
Esempio n. 13
0
    def __call__(self,username,password,*args,**kwargs):
    
        # ==================
        # PARSE THE USERNAME
        # ==================
    
        # Assume domains are passed with each username in one of three formats:
            # DOMAIN/USERNAME
            # DOMAIN\USERNAME
            # USERNAME@DOMAIN

        original_username = username
    
        if re.search(r'@',username):
            username, domain = username.split('@',username)
        elif re.search(r'/',username) or re.search(r'\\|\\',username):
            domain, username = re.split(r'/|\\',username)
        else:
            domain = self.default_domain

        conn = SMBConnection(username, password, self.client_name,
                self.server_name, domain=domain, use_ntlm_v2=True,
                is_direct_tcp=True)
        outcome = conn.connect(self.server_ip, self.server_port)

        # =============
        # RETURN OUTPUT
        # =============

        if outcome:
            conn.close()
            return (1,original_username,password,)
        else:
            return (0,original_username,password,)
Esempio n. 14
0
def main():
    """Main runner
    """
    global MAC_ADDRESS, IP_ADDRESS, REMOTE_NAME, SHARE_NAME

    # Load environment variables
    load_dotenv()
    if not has_valid_env():
        create_env()
    SAMBA_PATH = "{}/{}".format(REMOTE_NAME, SHARE_NAME)

    # Wake Computer
    send_magic_packet(MAC_ADDRESS)

    print("Waiting for computer to boot...")

    # Wait for share to load
    retry = True
    conn = SMBConnection("", "", "", REMOTE_NAME)
    while retry:
        try:
            conn.connect(IP_ADDRESS)
        except SMBTimeout:
            sys.exit(1)
        except NotConnectedError:
            pass
        except ConnectionRefusedError:
            pass
        else:
            conn.close()
            retry = False

    # Connect to Samba
    subprocess.run(["open", "smb://{}".format(SAMBA_PATH)], check=False)
Esempio n. 15
0
def connect(domain, username, password, client, server):
    try:
        conn = SMBConnection(username,
                             password,
                             client,
                             server,
                             domain=domain,
                             use_ntlm_v2=True,
                             is_direct_tcp=True)
        conn.connect('collabshare', 445)
        ip, port = conn.sock.getpeername()
        conn.close()
        print('{} INFO SUSPICIOUS_SMB_RESPONSE {} {}'.format(
            str(datetime.datetime.now()), ip, port))
        logfile(ip)
    except smb.smb_structs.ProtocolError as e:
        ip, port = conn.sock.getpeername()
        conn.close()
        print('{} INFO SUSPICIOUS_SMB_RESPONSE {} {}'.format(
            str(datetime.datetime.now()), ip, port))
        logfile(ip)
    except ConnectionRefusedError as e:
        pass
    except Exception as e:
        sys.stderr.write('{} ERROR {}'.format(str(datetime.datetime.now()),
                                              traceback.format_exc()))
Esempio n. 16
0
def get_thread(args, work, host):
    logger.debug('Connecting to {} as {}\\{}'.format(host, args.domain or '',
                                                     args.username))
    conn = SMBConnection(args.username,
                         args.password,
                         'adenum',
                         host,
                         use_ntlm_v2=True,
                         domain=args.domain,
                         is_direct_tcp=(args.smb_port != 139))
    conn.connect(host, port=args.smb_port)
    shares = [
        s.name.lower() for s in conn.listShares()
        if s.type == smb.base.SharedDevice.DISK_TREE
    ]
    for s in work[host]:
        if s.lower() in shares:
            for f in work[host][s]:
                local_path = (host + '\\' + f).replace('\\', '/')
                os.makedirs(os.path.dirname(local_path),
                            mode=0o770,
                            exist_ok=True)
                logger.info('Getting ' + host + '\\' + f)
                with open(local_path, 'wb') as fp:
                    conn.retrieveFile(s, f, fp)
    conn.close()
Esempio n. 17
0
    def action_verifica_connessione(self, cr, uid, ids, context=None):
        for id in ids:
            rec = self.browse(cr, uid, id, context=context)
            conn = None

            smb_domain = rec.smb_domain and rec.smb_domain or ''
            user = rec.user and rec.user or ''
            password = rec.password and rec.password or ''
            local_name = rec.title.encode('ascii', 'ignore')
            remote_name = rec.address.encode('ascii', 'ignore')
            path = rec.path
            errore = ''

            try:
                conn = SMBConnection(domain=smb_domain,
                                     username=user,
                                     password=password,
                                     my_name=local_name,
                                     remote_name=remote_name)
                esito_test = conn.connect(remote_name)
                self._directory_check(conn, rec.share, "/", path)
            except Exception as exception:
                esito_test = False
                errore = exception

            conn.close()

            if esito_test:
                rec.write({'state': 'confirmed'})
            else:
                raise orm.except_orm(_('Test di connessione fallito!'), errore)
Esempio n. 18
0
def get_version(source):
    host, port, username, password, encrypt = parse_source(source)

    log.info('lookup "%s" ...', host)
    ip = socket.gethostbyname(host)
    localhost = socket.gethostname()

    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!")

    # version magick
    log.info('get version ...')
    try:
        attr = conn.getAttributes(ADMIN_SERVICE, MAGICK_FILENAME)
        version = attr.last_write_time
    except OperationFailure:
        version = None

    conn.close()
    return format_version(version)
Esempio n. 19
0
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))
Esempio n. 20
0
def test_smb1():
    smb_structs.SUPPORT_SMB2 = smb_structs.SUPPORT_SMB2x = False
    info = getConnectionInfo()
    conn = SMBConnection(info['user'], info['password'], info['client_name'], info['server_name'], use_ntlm_v2 = True)
    assert conn.connect(info['server_ip'], info['server_port'])
    assert not conn.isUsingSMB2
    conn.close()
Esempio n. 21
0
    def samba_get(self, service_name, remotefile, localfile):
        samba = SMBConnection(self.username, self.password, '', '')
        samba.connect(self.ip, self.port)

        def list_get(share_name, remotestuff, localstuff):
            if os.path.isdir(localstuff) is False:
                os.mkdir(localstuff)
            for files in samba.listPath(share_name, remotestuff)[2:]:
                localpath = localstuff + os.sep + files.filename
                remotepath = remotestuff + '/' + files.filename
                if not samba.getAttributes(share_name, remotepath).isDirectory:
                    handler = open(localpath, 'wb')
                    samba.retrieveFile(share_name, remotepath, handler)
                    handler.close()
                else:
                    list_get(share_name, remotepath, localpath)
        try:
            if not samba.getAttributes(service_name, remotefile).isDirectory:
                handle = open(localfile, 'wb')
                samba.retrieveFile(service_name, remotefile, handle)
                handle.close()
            else:
                list_get(service_name, remotefile, localfile)
        except Exception:
            print('download file failure! ')
        finally:
            samba.close()
Esempio n. 22
0
    def download_file_by_smb(self, host, host_name, username, password, domain,
                             shared_folder_name, remote_file_path,
                             local_file_path):
        """
        download file by SMB
        :param host: IP
        :param host_name: 域名
        :param username: 用户名
        :param password: 密码
        :param domain: 域名
        :param shared_folder_name: 共享文件夹名称
        :param remote_file_path: 存放路径是相对共享文件夹的路径
        :param local_file_path: 本地文件路径
        :return:
        """
        try:
            remote_name = host_name or self.get_BIOSName(host)
            domain = domain or ''
            conn = SMBConnection(username,
                                 password,
                                 '',
                                 remote_name,
                                 domain,
                                 use_ntlm_v2=True)
            assert conn.connect(host)

            with open(local_file_path, 'wb') as f:
                conn.retrieveFile(shared_folder_name, remote_file_path, f)

        except Exception as e:
            raise SMBConnectionError(data=str(e))

        finally:
            conn.close()
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()
Esempio n. 24
0
    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()
Esempio n. 25
0
def main(argv):
    parser = argparse.ArgumentParser(description='Process SMB information.')
    parser.add_argument("-u", "--username")                 #argument to provide username
    parser.add_argument("-p", "--password")                 #argument to provide password
    parser.add_argument("-f", "--file", dest="filename")    #argument to provide file with a list of all systems you want to enumerate
    parser.add_argument("-d", "--domain")                   #argument to provide domain containing smb

    args = parser.parse_args()

###################################################################
# Opens a file that contains a list of system names to go through #
# and find all shares and the files with in those shares.         #
###################################################################

    with open(args.filename) as f:

        # Open file with system names
        for system_name in f:# Loop through file
            print('Enumerating over system: ' + system_name)
            conn = SMBConnection(args.username, args.password, 'enumerator', system_name, args.domain,
            use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=False)
            conn.connect(system_name, 139) # Attempt to connect to the system
            Shares = conn.listShares(timeout=30)  # Set Shares variable that contains list of shares
            print('Shares for: ' + system_name)
            for i in range(len(Shares)):  # iterate through the list of shares
                if "$" in Shares[i].name:
                    continue
                print("Share: ",i," =", Shares[i].name)
                Files = conn.listPath(Shares[i].name,'/',timeout=30) # Get a list of files in the share
                print('Files for: ' + system_name + '/' + "  Share: ",i," =",Shares[i].name)
                for i in range(len(Files)):
                    print("File[",i,"] =", Files[i].filename)
            conn.close()
Esempio n. 26
0
def run_brute_force(username, password, args):
	ip = args.ip
	port = args.port
	domain = args.domain
	list_shares = args.list_shares
	timeout = args.timeout
	verbose = args.verbose

	client_name = "client"
	server_name = ip
	if port == 445:
		is_direct_tcp = True
	else:
		is_direct_tcp = False

	try:
		# def __init__(self, username, password, my_name, remote_name, domain = '', use_ntlm_v2 = True, sign_options = SIGN_WHEN_REQUIRED, is_direct_tcp = False)
		conn = SMBConnection(username, password, client_name, server_name, domain = domain, use_ntlm_v2 = True, is_direct_tcp = is_direct_tcp)
		smb_authentication_successful = conn.connect(ip, port, timeout = timeout)
		if smb_authentication_successful:
			print "success: [%s:%s]" % (username, password)
			if list_shares:
				list_smb_shares(conn, timeout)
		else:
			if verbose:
				print "failed: [%s:%s]" % (username, password)
	except:
		if verbose:
			e = sys.exc_info()
			print "%s" % str(e)
	finally:
		if conn:
			conn.close()
Esempio n. 27
0
def startProtocol(port, user, passwd, ip, regex, target_path, args):
    global mirror, verbose, timeout
    mirror = args.search
    verbose = args.verbose
    timeout = args.timeout
    serverName = '*'
    conn = []
    direct = True
    if port != '445':
        serverName = input(
            "SMB over TCP is used by default on port 445.\nIf you prefer NetBIOS instead, you need to provide the NetBIOS name of the remote machine: "
        )
        direct = False
    try:
        conn = SMBConnection(user,
                             passwd,
                             'noone',
                             serverName,
                             use_ntlm_v2=True,
                             is_direct_tcp=direct)
        stdout.write("Trying SMB server: %s......" % str(ip))
        stdout.flush()
        conn.connect(str(ip), int(port), timeout=timeout)
        print("[Connected]")
        shares(target_path, conn, regex)
        conn.close()
    except OSError as err:
        print("[No connection]")
        if verbose > 0: print(err.message)
        conn.close()
        pass
    def test_003_samba(self):
        u'''samba服务器(部分型号暂未支持)'''

        host = gettelnet('host')
        ftpSmaba = NetworkSharingPage(self.driver, self.url)
        #1、未开启验证无法登录
        user_name = "anonymous"
        pass_word = ""
        my_name = "anyname"
        domain_name = ""
        remote_smb_IP = host
        smb = SMBConnection(user_name,
                            pass_word,
                            my_name,
                            domain_name,
                            use_ntlm_v2=True)
        try:
            smb.connect(remote_smb_IP, 139, timeout=3)
        except ConnectionRefusedError:
            print('未开启samba 无法访问 验证通过')

        #2、打开samba
        sambaEn = ftpSmaba.getAttribute_byName(ftpSmaba.sambaEnS, 'checked')
        if sambaEn != 'true':
            ftpSmaba.click_sambaEn()
            ftpSmaba.click_save()
        time.sleep(13)
        sambaEn = str(
            ftpSmaba.getAttribute_byName(ftpSmaba.sambaEnS, 'checked'))
        self.assertEqual(sambaEn, 'true', msg='samba开启 失败')
        #samba登录
        smb = SMBConnection(user_name,
                            pass_word,
                            my_name,
                            domain_name,
                            use_ntlm_v2=True)
        try:
            smb.connect(remote_smb_IP, 139, timeout=3)
        except socket.timeout:
            raise Exception('samba服务无法访问')
        shareslist = smb.listShares()  # 列出共享目录 这里只能看到1级菜单“1”
        smb.close()
        n = []
        for i in shareslist:
            n.append(i.name)
        print(n)
        nn = []
        for x in n:
            if '$' not in x:
                nn.append(x)
                print(nn)
        #3、打开验证
        if '1' in nn:
            pass
        else:
            raise Exception('samba验证失败')  # 如果没有则报错

        self.driver.quit()
        logger.info('test_003_samba passed')
Esempio n. 29
0
def show_dir(path):
    conn = SMBConnection(USERNAME, PASSWORD, MY_NAME, REMOTE_NAME, use_ntlm_v2=False)
    conn.connect(SERVER_IP, PORT)
    re = conn.listPath('Share',  os.path.join('/ESAP/Hand-Out/', path))
    conn.close()
    for i in re:
        i.link = os.path.join(path, i.filename)
    return render_template('hello.html', files=re)
Esempio n. 30
0
class SambaConnection():
    """
    Retorna conexão no servidor de arquivo SMB. Para ser usado com
    contexto with.

    Args:
        - samba_conn_id (str): Id da conexão (Airflow) do servidor de
        arquivo SMB. A conexão deve: 1) Utilizar o 'NetBIOS Name' do servidor
        como host; 2) Conter parâmetro extra "{'domain_name': 'MP'}" para
        autenticação com usuário da rede do ministério; 3) Conter parâmetro
        extra 'service_name'; 4) Utilizar parâmetro extra 'IP' para conexão
        através da VPN.
        Ex.: {
               "domain_name": "MP",
               "service_name": "dadossiconv",
               "IP": "10.209.8.101"
             }

    Return:
        - conn: conexão com o servidor de arquivos
        - service_name: nome do serviço cadastrado no Id da conexão (Airflow)
    """
    def __init__(self, samba_conn_id):
        samba_conn_values = BaseHook.get_connection(samba_conn_id)
        self.user_id = samba_conn_values.login
        self.password = samba_conn_values.password
        self.client_machine_name = 'airflow_server'
        self.server_name = samba_conn_values.host
        self.domain_name = samba_conn_values.extra_dejson.get('domain_name')
        self.service_name = samba_conn_values.extra_dejson.get('service_name')
        self.host_ip = samba_conn_values.extra_dejson.get('IP', '')
        self.port = int(samba_conn_values.port)

    def __enter__(self):
        try:
            self.conn = SMBConnection(self.user_id,
                                      self.password,
                                      self.client_machine_name,
                                      self.server_name,
                                      domain=self.domain_name,
                                      use_ntlm_v2=True)
            self.conn.connect(self.server_name, self.port)
            return self.conn, self.service_name
        except:
            try:
                self.conn = SMBConnection(self.user_id,
                                          self.password,
                                          self.client_machine_name,
                                          self.server_name,
                                          domain=self.domain_name,
                                          use_ntlm_v2=True)
                self.conn.connect(self.host_ip, self.port)
                return self.conn, self.service_name
            except:
                raise Exception('Samba connection failed.')

    def __exit__(self, exc_type, exc_value, traceback):
        self.conn.close()
Esempio n. 31
0
def lambda_handler(event, context):

    server = os.environ['DCS_SMB_HOST']
    res = dns.resolver.Resolver(configure=False)
    res.nameservers = ['1.1.24.68', '1.1.1.51']
    dns_response = res.query(server, 'a')

    share = "Network"
    port = 445
    domain = 'meteo'
    username = os.environ['DCS_SMB_USER']
    password = os.environ['DCS_SMB_PASS']
    file_name = "DCs.csv"

    s3_client = boto3.client('s3')
    bucket_name = os.environ['PA_EDL_AWS_S3_BUCKET']
    path = 'iprange/'
    out_file = 'meteo-dcs.txt'

    try:
        if dns_response:
            server = dns_response[0].address
        else:
            raise ValueError("Unable to resolve server's DNS record")
        conn = SMBConnection(username,
                             password,
                             'AWS:LAMBDA:PA_EDL',
                             server,
                             domain,
                             is_direct_tcp=True)
        conn.connect(server, 445)
        file_obj = tempfile.NamedTemporaryFile(mode='w+b', delete=False)
        file_attributes, copysize = conn.retrieveFile(share, file_name,
                                                      file_obj)
        file_obj.seek(0)
        data = file_obj.read().decode('utf-8')
        ips = re.findall('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\/\d{1,2})?',
                         data)
        if ips:
            ips = [[text.strip('"') for text in line.split(',')]
                   for line in data.split()[1:]]
            payload = '\n'.join(set(f"{entry[1]} #{entry[0]}"
                                    for entry in ips))
            s3_client.put_object(Bucket=bucket_name,
                                 Key=path + out_file,
                                 Body=payload,
                                 ContentType='text/html')
        else:
            raise ValueError('Unable to extract IP info')
    except Exception as e:
        logger.exception('Error occurred: ' + str(e))
        exit(1)
    finally:
        file_obj.close()
        conn.close()

    logger.info('Successful Invocation')
Esempio n. 32
0
def connectSmb():
    try:
        global smbconn
        global smbstatue
        smbconn = SMBConnection(username, password, 'xu-All-Series','WIN-FILE1',"jlinksz.com", use_ntlm_v2=True)
        smbstatue = smbconn.connect("192.168.8.206", 139)
    except Exception as e:
        print(e)
        smbconn.close()
Esempio n. 33
0
 def make_directory(self, service_name, path):
     samba = SMBConnection(self.username, self.password, '', '')
     samba.connect(self.ip, self.port)
     try:
         samba.createDirectory(service_name, path)
     except Exception:
         print('create directory failure! ')
     else:
         print('create directory success! ')
     finally:
         samba.close()
Esempio n. 34
0
def dotransform(args):
    mt = MaltegoTransform()
    # mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    workgroup = mt.getVar("workgroup")
    account = mt.getVar("account_used")
    path = mt.getVar("path")
    domaindns = mt.getVar("domain_dns")
    sharename = mt.getVar("sharename")

    conn = SMBConnection('',
                         '',
                         "localhost",
                         server,
                         domain=workgroup,
                         use_ntlm_v2=True,
                         is_direct_tcp=True)
    conn.connect(ip, int(port))
    regex = re.compile("^\.{1,2}$")
    for file in conn.listPath(sharename, path):
        filename = unicodedata.normalize("NFKD", file.filename).encode(
            'ascii', 'ignore')
        if file.isDirectory:
            if not regex.match(filename):
                entityname = "msploitego.SambaShare"
                newpath = "{}/{}".format(path, filename)
            else:
                continue
        else:
            entityname = "msploitego.SambaFile"
            newpath = "{}/{}".format(path, filename)
        sambaentity = mt.addEntity(entityname,
                                   "{}/{}{}".format(ip, sharename, newpath))
        sambaentity.setValue("{}/{}{}".format(ip, sharename, newpath))
        sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
        sambaentity.addAdditionalFields("port", "Port", False, port)
        sambaentity.addAdditionalFields("server", "Server", False, server)
        sambaentity.addAdditionalFields("workgroup", "Workgroup", False,
                                        workgroup)
        sambaentity.addAdditionalFields("filename", "Filename", False,
                                        filename)
        sambaentity.addAdditionalFields("path", "Path", False, newpath)
        sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
        sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False,
                                        domaindns)
        sambaentity.addAdditionalFields("sharename", "Share Name", False,
                                        sharename)
    conn.close()
    mt.returnOutput()
    mt.addUIMessage("completed!")
Esempio n. 35
0
def getFileList():
    filename_list = []
    conn = SMBConnection(smb_credential['user'], smb_credential['password'], "", smb_credential['hostname'], use_ntlm_v2 = True)
    conn.connect(smb_credential['host'], 139)

    file_list = conn.listPath(short_group_name, u'/Audit/每周会议记录/%s-%s年%s学期会议记录' % (short_group_name, cur_year, cur_semester))
    for f in file_list:
        if f.filename != '.' and f.filename != '..':
            filename_list.append(f.filename)

    conn.close()
    return filename_list
Esempio n. 36
0
def dotransform(args):
    mt = MaltegoTransform()
    # mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    if not server:
        server = mt.getVar("machinename")
    workgroup = mt.getVar("workgroup")
    path = mt.getVar("path")
    domaindns = mt.getVar("domain_dns")
    sharename = mt.getVar("sharename")

    if not workgroup:
        workgroup = "WORKGROUP"
    # conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True,is_direct_tcp=True)
    conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True)
    conn.connect(ip, int(port))
    regex = re.compile("^\.{1,2}$")
    try:
        files = conn.listPath(sharename, path)
    except NotReadyError:
        accessdenied = mt.addEntity("msploitego.AccessDenied",sharename)
        accessdenied.setValue(sharename)
    else:
        for file in files:
            filename = unicodedata.normalize("NFKD", file.filename).encode('ascii', 'ignore')
            if file.isDirectory:
                if not regex.match(filename):
                    entityname = "msploitego.SambaShare"
                    newpath = "{}/{}".format(path,filename)
                else:
                    continue
            else:
                entityname = "msploitego.SambaFile"
                newpath = "{}/{}".format(path, filename)
            sambaentity = mt.addEntity(entityname,"{}/{}{}".format(ip,sharename,newpath))
            sambaentity.setValue("{}/{}{}".format(ip,sharename,newpath))
            sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
            sambaentity.addAdditionalFields("port", "Port", False, port)
            sambaentity.addAdditionalFields("server", "Server", False, server)
            sambaentity.addAdditionalFields("workgroup", "Workgroup", False, workgroup)
            sambaentity.addAdditionalFields("filename", "Filename", False, filename)
            sambaentity.addAdditionalFields("path", "Path", False, newpath)
            sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
            if domaindns:
                sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False, domaindns)
            sambaentity.addAdditionalFields("sharename", "Share Name", False, sharename)
    conn.close()
    mt.returnOutput()
    mt.addUIMessage("completed!")
Esempio n. 37
0
def show_file(filename):
    conn = SMBConnection(USERNAME, PASSWORD, MY_NAME, REMOTE_NAME, use_ntlm_v2=False)
    conn.connect(SERVER_IP, PORT)
    #This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects). (For standard strings, see str and unicode.)
    temp_fh = StringIO()
#file_obj  A file-like object that has a write method. Data will be written continuously to file_obj until EOF is received from the remote service. In Python3, this file-like object must have a write method which accepts a bytes parameter.
    file_attributes, filesize = conn.retrieveFile('Share', '/ESAP/Hand-Out/' + filename, temp_fh)
    conn.close()
    #读取文件名字
    localfile = filename.split('/')[-1]
#存到服务器
    f = open(os.path.join(os.getcwd() + '/static/', localfile), 'w')
    f.write(temp_fh.getvalue())
#读取服务器的文件
    return redirect(url_for('static', filename=localfile), code=301)
Esempio n. 38
0
def login(worker):
	finished=False
	conn=SMBConnection(userID, passwords[worker], 'client_machine_name', server_name, use_ntlm_v2 = True)
	while not finished:
		try:
			if conn.connect(server_ip, 139,timeout=100):
				rightPass=password[worker]
				conn.close()
			finished=True
		except Exception as e:
			if "An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full" in str(e):
				finished=False
				time.sleep(1)
			else:
				print(e)
	del conn
Esempio n. 39
0
 def run(self):
    print "Starting thread for " + self.ip
    net = NetBIOS()
    net_name = str(net.queryIPForName(self.ip)).strip("['").strip("']")
    net.close()
    conn = SMBConnection(self.user, self.pwd, 'cobwebs', net_name, domain=self.domain, use_ntlm_v2 = False)
    if conn.connect(self.ip, port=139, timeout=10):
       print ("Connecting to %s was successful! How about a nice game of spidering %s%s?" % (self.ip, self.share, self.subfolder))
    else:
       print ("Connection error: %s" % (self.ip))
    if self.recursive > 0:
       recurse(conn,self.ip,self.share,self.subfolder,self.pattern,int(self.recursive))    
    else:
       filelist = conn.listPath(self.share, self.subfolder)
       dir_list(filelist,self.ip,self.subfolder,self.pattern)
    conn.close()
    print "Exiting thread for " + self.ip
Esempio n. 40
0
def main():
    parser = argparse.ArgumentParser('check_smbproxy')
    parser.add_argument('server_ip', metavar='server-ip')
    parser.add_argument('server_port', metavar='server-port', type=int)
    parser.add_argument('server_name', metavar='server-name')
    parser.add_argument('share_name', metavar='share-name')
    parser.add_argument('--path', default='/')
    parser.add_argument('--user', default='cluster_user')
    parser.add_argument('--password', default='cluster_user_password')


    parsed_args = parser.parse_args()

    userID = parsed_args.user
    password = parsed_args.password
    client_machine_name = 'test_client'

    server_ip = parsed_args.server_ip
    server_port = parsed_args.server_port
    server_name = parsed_args.server_name
    share_name = parsed_args.share_name
    path = parsed_args.path

    try:
        start_time = datetime.datetime.utcnow()
        conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2=False, is_direct_tcp=True)
        assert conn.connect(server_ip, server_port)

        ls = conn.listPath(share_name, path)
        num_files = len(ls)
#        for f in ls:
#            print f.filename

        conn.close()
        end_time = datetime.datetime.utcnow()
        time_spent = (end_time-start_time).total_seconds()*1000

        print "OK: %d files found in %s | connection_time=%dms" % (num_files, path, time_spent)
    except Exception:
        print "CRITICAL: Exception while trying to connect:"
        print traceback.print_exc()
        sys.exit(2)

    sys.exit(0)
Esempio n. 41
0
def test_smb(ip, port, credentials):
    success = []
    nb = NetBIOS.NetBIOS(broadcast=False)
    try:
        # names = nb.queryIPForName(ip,139,timeout=10)
        # if names == None or names == []:
        #     name = ""
        # else:
        #     name = names[0]
        for login in credentials:
            password = credentials[login]
            conn = SMBConnection(login, password, "PwnieXpress", "")
            result = conn.connect(ip,port=port,timeout=30)
            if result:
                success.append([login,password])
                conn.close()
    except Exception as e:
        print(e)
    nb.close()
    return success
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))
Esempio n. 43
0
	def getNetworkShares(self, hostip, hostname):
		sharelist = []
		self.sharecache_file = None
		self.sharecache_file = eEnv.resolve("${sysconfdir}/enigma2/") + hostname.strip() + '.cache' #Path to cache directory

		username = "******"
		password = ""
		if os_path.exists(self.sharecache_file):
			print '[Networkbrowser] Loading userinfo from ',self.sharecache_file
			try:
				self.hostdata = load_cache(self.sharecache_file)
				username = self.hostdata['username']
				password = self.hostdata['password']
			except:
				pass

		for port in (445, 139):
			try:
				smbconn = SMBConnection(username, password, self._myhostname, hostname, is_direct_tcp=(port == 445))
				if smbconn.connect(hostip, port=port, timeout=1):
					print '[Networkbrowser] established SMB connection to %s:%d' % (hostip, port)
					for share in smbconn.listShares(timeout=1):
						if share.type == SharedDevice.DISK_TREE and not share.isSpecial:
							sharelist.append( NetworkItemSmb(hostname, hostip, share.name.encode('utf-8'), 'Disk', comment=share.comments.encode('utf-8')) )
					smbconn.close()
					break
			except Exception as e:
				Log.w('[Networkbrowser] SMBConnection: ' + str(e))
		try:
			exports = showmount(hostip)
		except IOError as e:
			Log.w('[Networkbrowser] showmount: ' + str(e))
		else:
			for ex in exports:
				sharelist.append( NetworkItemNfs(os_path.basename(ex['dir']), hostip, ex['dir'], ','.join(ex['groups'])) )
		return sharelist
Esempio n. 44
0
def startProtocol(port, user, passwd, ip, regex, target_path, args):
    global mirror, verbose, timeout
    mirror = args.search
    verbose = args.verbose
    timeout = args.timeout
    serverName = '*'
    conn = []
    direct = True
    if port != '445':
        serverName = input("SMB over TCP is used by default on port 445.\nIf you prefer NetBIOS instead, you need to provide the NetBIOS name of the remote machine: ")
        direct = False
    try:
        conn = SMBConnection(user, passwd, 'noone', serverName, use_ntlm_v2 = True, is_direct_tcp = direct)
        stdout.write("Trying SMB server: %s......" % str(ip))
        stdout.flush()
        conn.connect(str(ip), int(port), timeout = timeout)
        print("[Connected]")
        shares(target_path, conn, regex)
        conn.close()
    except OSError as err:
        print("[No connection]")
        if verbose > 0: print(err.message)
        conn.close()
        pass
Esempio n. 45
0
        raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS IP failed')
    target_ip = ips[0]

if target_nb_name is None:
    print('Looking up NetBIOS name from target IP: ' + target_ip)
    nb_names = nb.queryIPForName(target_ip)
    print('Got NB names: ' + str(nb_names))
    if nb_names is None or len(nb_names) < 1:
        raise RuntimeError('Cannot connect to host ' + target + '; looking up NetBIOS name failed')
    target_nb_name = nb_names[0]

nb.close()

client_machine_name = socket.gethostbyaddr(socket.gethostname())[0]
# client_machine_name = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(15))
# print('Generated client machine name: ' + client_machine_name + '\n')

domain = input('Enter domain [none]: ')
username = input('Enter username: '******'Enter password: '******'Could not connect to host ' + target + '; establishing connection failed')

if conn.echo('blah') != 'blah':
    raise RuntimeError('Connection test (echo) failed')

conn.close()
Esempio n. 46
0
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)
Esempio n. 47
0
    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()
Esempio n. 48
0
class SMBFS(FS):
    """ Filesystem stored on a SMB share.

        This wraps pysmb (https://pypi.python.org/pypi/pysmb) to access SMB
        shares.
    """
    _meta = {'thread_safe': True,
             'virtual': False,
             'read_only': False,
             'unicode_paths': True,
             'case_insensitive_paths': False,  # It depends upon the server.
             'network': True,
             'atomic.makedir': True,
             'atomic.removedir': False,
             'atomic.rename': True,
             'atomic.setcontents': False,
             'mime_type': 'virtual/smb'}

    def __init__(self, username, password, server_name, server_IP, share,
                 port=139, client_name=None,
                 thread_synchronize=_thread_synchronize_default):
        self.username = username
        self.password = password
        self.server_name = server_name
        self.share = share
        self.server_IP = server_IP
        self.port = port
        self._conn = None

        # Automatically generate a client name if not provided.
        if client_name is None:
            self.client_name = 'fs{0}'.format(''.join(random.choice(
                string.uppercase + string.digits) for i in xrange(12)))
        else:
            self.client_name = client_name

        super(SMBFS, self).__init__(thread_synchronize=thread_synchronize)

    def __getstate__(self):
        # Close the connection to allow pickling.
        self.close()
        return super(SMBFS, self).__getstate__()

    def _listPath(self, path, list_contents=False):
        """ Path listing with SMB errors converted. """
        # Explicitly convert the SMB errors to be able to catch the
        # PyFilesystem error while listing the path.
        if list_contents:
            try:
                # List all contents of a directory.
                return _conv_smb_errors(self.conn.listPath)(
                    self.share, normpath(path))
            except ResourceNotFoundError:
                if self.isfile(path):
                    raise ResourceInvalidError(path)
                raise
        else:
            # List a specific path (file or directory) by listing the contents
            # of the containing directory and comparing the filename.
            pathdir = dirname(path)
            searchpath = basename(path)
            for i in _conv_smb_errors(self.conn.listPath)(self.share, pathdir):
                if i.filename == '..':
                    continue
                elif ((i.filename == '.' and searchpath == '') or
                      i.filename == searchpath):
                    return i
            raise ResourceNotFoundError(path)

    @_conv_smb_errors
    def _retrieveFile(self, path, file_obj):
        """ Retrieve a file.  Convert SMB errors. """
        # Retrieve a file then rewind it to the beginning as pysmb leaves it at
        # the end of the file.
        self.conn.retrieveFile(self.share, path, file_obj)
        file_obj.seek(0)

    @_conv_smb_errors
    def _rename(self, src, dst):
        """ Rename a path.  Convert SMB errors. """
        self.conn.rename(self.share, src, dst)

    @_conv_smb_errors
    def _create_dir(self, path):
        """ Create a directory.  Convert SMB errors. """
        self.conn.createDirectory(self.share, path)

    @_conv_smb_errors
    def _remove_dir(self, path):
        """ Remove a directory.  Convert SMB errors. """
        self.conn.deleteDirectory(self.share, path)

    @synchronize
    @_conv_smb_errors
    @_absnorm_path(1)
    def setcontents(self, path, data=b'', encoding=None, errors=None,
                    chunk_size=1024 * 64):
        # Remove then write contents.  There is no method to erase the contents
        # of a file when writing to it using pysmb.
        try:
            self.remove(path)
        except ResourceNotFoundError:
            pass

        if not hasattr(data, 'read'):
            data = StringIO(data)
        self.conn.storeFile(self.share, path, data)

    @property
    @synchronize
    @_conv_smb_errors
    def conn(self):
        """ Connection to server. """
        if self._conn is None:
            self._conn = SMBConnection(
                self.username, self.password, self.client_name,
                self.server_name, use_ntlm_v2=True)
            self._conn.connect(self.server_IP, self.port)
        return self._conn

    @synchronize
    def close(self):
        super(SMBFS, self).close()
        if self._conn is not None:
            self._conn.close()
            self._conn = None

    @synchronize
    @iotools.filelike_to_stream
    @_absnorm_path(1)
    def open(self, path, mode='r', **kwargs):
        if self.isdir(path):
            raise ResourceInvalidError(path)

        # Erase the contents of a file upon write.
        if 'w' in mode:
            file_obj = None
            self.setcontents(path, StringIO())
        else:
            file_obj = SpooledTemporaryFile()
            self._retrieveFile(path, file_obj)
        return RemoteFileBuffer(self, path, mode, file_obj)

    @synchronize
    @_absnorm_path(1)
    def isfile(self, path):
        try:
            return not self._listPath(path).isDirectory
        except FSError:
            return False

    @synchronize
    @_absnorm_path(1)
    def isdir(self, path):
        try:
            return self._listPath(path).isDirectory
        except FSError:
            return False

    def _conv_smb_info_to_fs(self, smb_info):
        """ Convert SMB information into PyFilesystem info dict. """
        return {'size': smb_info.file_size,
                'st_mode': (stat.S_IFDIR if smb_info.isDirectory else
                            stat.S_IFREG),
                'created_time': datetime.datetime.fromtimestamp(
                    smb_info.create_time),
                'st_ctime': smb_info.create_time,
                'accessed_time': datetime.datetime.fromtimestamp(
                    smb_info.last_access_time),
                'st_atime': smb_info.last_access_time,
                'modified_time': datetime.datetime.fromtimestamp(
                    smb_info.last_write_time),
                'st_mtime': smb_info.last_write_time}

    @synchronize
    def listdirinfo(self, path="./", wildcard=None, full=False, absolute=False,
                    dirs_only=False, files_only=False):
        listing = []
        for i in self._listPath(path, list_contents=True):
            # Skip ., .. and undesired types.
            if (i.filename == '.' or i.filename == '..' or
                (dirs_only and not i.isDirectory) or
                (files_only and i.isDirectory)):
                continue

            # Rely on the PyFilesystem helper to determine if the path should
            # be listed.  An empty listing indicates to not list the path.
            name = self._listdir_helper(path, [i.filename], wildcard, full,
                                        absolute, False, False)
            if len(name) == 1:
                listing.append((name[0], self._conv_smb_info_to_fs(i)))
        return listing

    @synchronize
    def listdir(self, path="./", wildcard=None, full=False, absolute=False,
                dirs_only=False, files_only=False):
        # Wrap whatever listdirinfo returns while discarding the info.
        return [name[0] for name in self.listdirinfo(
            path, wildcard, full, absolute, dirs_only, files_only)]

    @synchronize
    def makedir(self, path, recursive=False, allow_recreate=False):
        # Create a directory from the top downwards depending upon the flags.
        paths = recursepath(path) if recursive else (path, )
        for p in paths:
            if p == '/':
                continue

            # Try to create a directory first then ask for forgiveness.
            try:
                self._create_dir(p)
            except DestinationExistsError as e:
                if self.isfile(p):
                    raise ResourceInvalidError(path)
                elif self.isdir(p):
                    if not recursive and not allow_recreate:
                        raise DestinationExistsError(path)
            except ResourceNotFoundError as e:
                if not recursive and not self.isdir(dirname(p)):
                    raise ParentDirectoryMissingError(path)
                e.path = path
                raise
            except FSError as e:
                e.path = path
                raise

    @synchronize
    @_conv_smb_errors
    @_absnorm_path(1)
    def remove(self, path):
        self.conn.deleteFiles(self.share, path)

    @synchronize
    @_conv_smb_errors
    @_absnorm_path(1)
    def removedir(self, path, recursive=False, force=False):
        if path == '/':
            raise RemoveRootError(path)

        # Remove directory tree from the bottom upwards depending upon the
        # flags.
        if force:
            for (del_dir, del_files) in self.walk(path, search='depth',
                                                  ignore_errors=True):
                for f in del_files:
                    self.remove(pathjoin(del_dir, f))
                self.removedir(del_dir)
        elif recursive:
            paths = recursepath(path, reverse=True)[:-1]
            for p in paths:
                self._remove_dir(p)
        else:
            self._remove_dir(path)

    @synchronize
    @_absnorm_path(2)
    @_determine_cause
    def rename(self, src, dst):
        self._rename(src, dst)

    @synchronize
    @_absnorm_path(1)
    def getinfo(self, path):
        return self._conv_smb_info_to_fs(self._listPath(path))
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
Esempio n. 50
0
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()
Esempio n. 51
0
class FetchFile(FetchDataBase):
	_localFolder = ''
	_serverName = ''
	_rootNode = ''
	_subPath = ''
	_conn = None
	_filename = ''
	#_folderList = []
	#_fileList = []
	def __init__(self, addr, uid, upwd):
		#super(FetchFile, self).__init__(addr, uid, upwd)
		# print 'addr:', addr
		try:
			# FetchDataBase.__init__(self, r'//CNST50066074/Root/Business_One/Projects/Dev/SDK/KnowledgeWarehouse/_Ebook/java/m2ebook-pdf.pdf', uid.lower(), upwd)
			FetchDataBase.__init__(self, str(addr), uid.lower(), upwd)
			self._genPath()
		except Exception as e:
			self._responseCode = 430
			print 'init error....'
			raise e

	def _genPath(self):
		rootPath = self._addr
		self._localFolder = self._addr
		#skip first domain path, ie "\\servername\path\
		# print 'self._addr:', self._addr
		if self._addr.startswith(PLACEHOLDER_START):
			if len(self._addr) <= 2:
				raise Exception('not support"', self._addr, '"root path')
			else:
				rootPath = self._addr[2:]

			#get servername
			index = rootPath.find(PLACEHOLDER)
			if index > 0:
				self._serverName = rootPath[0:index]
				rootPath = rootPath[index+1:]
			else:
				#ie \\servername\
				self._serverName = rootPath
				rootPath = ''
		else:
			raise Exception('invalid net work path')

		#local temp path
		self._localFolder = base64.b64encode(self._addr)

		#find root path, ie path\
		index = rootPath.find(PLACEHOLDER)
		if index > 0:
			self._rootNode = rootPath[0:index]
			self._subPath = rootPath[index:]
		else:
			self._rootNode = rootPath
			self._subPath = ''
			#raise Exception('invalid path ')
		#print 'rootNode', self._rootNode
		#print 'subpath', self._subPath

	def _connToServer(self):
		try:
			#print 'id:', self._uid
			# print 'servername', self._serverName
			self._conn = SMBConnection(self._uid, self._upwd, '', self._serverName) #use_ntlm_v2 = True
			if not self._conn.connect(self._serverName): #port = 139
				raise Exception('connect failure:', self._serverName)
		except Exception as e:
			print str(e)

	def _connClose(self):
		self._conn.close()

	FOLDERLIST = 'folderlist'
	FILELIST = 'filelist'
	def _getFileList(self, subFolder = ''):
		if subFolder == '':
			subFolder = self._subPath
		print 'rootnode:', self._rootNode
		print 'subFolder:', subFolder
		list = self._conn.listPath(self._rootNode, subFolder)
		folderList = []
		fileList = []
		for f in list:
			if f.filename in ['.','..','Thumbs.db']:
				continue
			if f.isDirectory:
				folderList.append(f.filename)
			else:
				fileList.append(f.filename)
		folderList.append('')
		#print folderList, fileList
		return {self.FOLDERLIST:folderList, self.FILELIST:fileList}

	def _getLastFilePath(self, path):
		index = path.rfind(PLACEHOLDER)
		#print 'getlastfilepath index:', index
		if index < 0:
			return {'path':'', 'file':path}
			#raise Exception('not found last path')
		if index + 1 == len(path):
			#print 'index', index
			#print 'len(path):', len(path), path
			path = path[0:index]
			return self._getLastFilePath(path)
		else:
			return {'path':path[0:index+1], 'file':path[index+1:]}


	#TYPE_FOLDER = 'folder'
	#TYPE_FILE = 'file'
	# folder or file
	def _getFileType(self):
		#get path
		dpath = self._getLastFilePath(self._subPath)
		path = dpath['path']
		file = dpath['file']
		#print 'path', path
		#print 'file', file
		self._filename = file
		#
		dlist = self._getFileList(path)
		folderList = dlist[self.FOLDERLIST]
		fileList = dlist[self.FILELIST]

		if file.decode('utf-8') in folderList:
			self._resourceType = TYPE_FOLDER
			return TYPE_FOLDER
		if file.decode('utf-8') in fileList:
			self._resourceType = TYPE_FILE
			return TYPE_FILE
		return None

	#if folder return filelist, if file return filepath.
	def _getContent(self):
		type = self._getFileType()
		#print 'content type', type
		if type == TYPE_FOLDER:
			dfiles = self._getFileList()
			#print 'dfiles', dfiles
			#dfiles['type'] = TYPE_FOLDER
			files = []
			files.extend(dfiles[self.FOLDERLIST])
			files.extend(dfiles[self.FILELIST])
			return TYPE_FOLDER, str(files)
		elif type == TYPE_FILE:
			# return {'filepath':self._retrieveFile()}
			tempResult = self._retrieveFile()
			# return TYPE_FILE, self._retrieveFile()
			return TYPE_FILE, tempResult
		else:
			return None, None

	def _retrieveFile(self):
		tempPath = SF_TEMPPATH
		self.mkdir(tempPath)
		import os
		filename =  self._addr.replace('/', '_')
		tempPath = os.path.realpath(tempPath + filename)
		import tempfile
		with open(tempPath, 'wb+') as fo:
			#fp = tempfile.NamedTemporaryFile()
			fa, filesize = self._conn.retrieveFile(self._rootNode, self._subPath, fo)
			#print 'filesize', filesize
			#for line in fp:
			#	fo.write(line)
			#fp.close()
			fo.close()
		return tempPath

	def _detectFileType(self, fileType):
		from const_file_type import GLOBAL_FILE_TYPE
		if fileType == TYPE_FOLDER:
			return GLOBAL_FILE_TYPE.SF
		elif fileType == TYPE_FILE:
			if self._addr.lower().endswith('.pdf'):
				return GLOBAL_FILE_TYPE.PDF
			elif self._addr.lower().endswith('.doc'):
				return  GLOBAL_FILE_TYPE.DOC
			elif self._addr.lower().endswith('.docx'):
				return GLOBAL_FILE_TYPE.DOCX
			elif self._addr.lower().endswith('.ppt'):
				return GLOBAL_FILE_TYPE.PPT
		return GLOBAL_FILE_TYPE.OTHERS

	def fetchData(self):
		try:
			self._connToServer()
			fileType, content = self._getContent()
			#print 'content', content
			self._connClose()
			if content == None:
				self._responseCode = 430
				e = Exception()
				e.code = 430
				e.message = "Wrong url, can not fetch url"
				raise e
			self._responseCode = 200
			return content, self._addr, self._detectFileType(fileType)
		except Exception as e:
			self._responseCode = 430
			e.code = 430
			raise e


	#using mount to open share folder, not working at now.
	def _openShareFolder(self):
		#//10.58.0.100/Root/Business_One/Projects/Dev/SDK/KnowledgeWarehouse/_Ebook/Effective STL.pdf
		tempPath = "../temp/"
		tempPath += self._localFolder
		self.mkdir(tempPath)

		#print 'absolute path'
		tempPath = os.path.realpath(tempPath)
		print tempPath

		command = r"sudo mount.cifs " + self._addr + " " + tempPath + "/ -o user="******",password="
		print command
		try:
			os.system(command + self._upwd)
		except Exception, e:
			print e
			raise e