13

你能给我一个使用 pysmb 库连接到一些 samba 服务器的例子吗?我读过有类 smb.SMBConnection.SMBConnection(username, password, my_name, remote_name, domain='', use_ntlm_v2=True) 但我不知道如何使用它

4

3 回答 3

17

我最近一直在使用 pysmb 进行网络共享枚举,发现找到好的/完整的例子并不容易。我建议您参考我为枚举 smb 与 pysmb 共享而编写的一个小脚本:https ://github.com/n3if/scripts/tree/master/smb_enumerator

另外,为了完整起见,我在这里发布完成连接和枚举的代码片段:

from smb import SMBConnection

try:
    conn = SMBConnection(username,password,'name',system_name,domain,use_ntlm_v2=True,
                         sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
                         is_direct_tcp=True) 
    connected = conn.connect(system_name,445)

    try:
        Response = conn.listShares(timeout=30)  # obtain a list of shares
        print('Shares on: ' + system_name)

        for i in range(len(Response)):  # iterate through the list of shares
            print("  Share[",i,"] =", Response[i].name)

            try:
                # list the files on each share
                Response2 = conn.listPath(Response[i].name,'/',timeout=30)
                print('    Files on: ' + system_name + '/' + "  Share[",i,"] =",
                                       Response[i].name)
                for i in range(len(Response2)):
                    print("    File[",i,"] =", Response2[i].filename)
            except:
                print('### can not access the resource')
    except:
        print('### can not list shares')    
except:
    print('### can not access the system')
于 2015-05-08T14:25:58.130 回答
8

SMBConnection 类将允许您以阻塞模式访问远程 Samba 服务器上的文件。

要检索远程服务器上共享文件夹中的文件列表,

from smb.SMBConnection import SMBConnection
conn = SMBConnection(userid, password, client_machine_name, remote_machine_name, use_ntlm_v2 = True)
conn.connect(server_ip, 139)
filelist = conn.listPath('shared_folder_name', '/')

返回的文件列表将是SharedFile实例列表。

更多示例可以tests/SMBConnectionTests在 pysmb 源包中的文件夹中找到。

于 2012-04-25T04:57:14.723 回答
1

例如,您想通过 pysmb 存储一个文件,如下所示:

from smb.SMBConnection import SMBConnection

file_obj = open('image.png', 'rb')

connection = SMBConnection(username=username,
                           password=password,
                           remote_name=remote_name,  # It's net bios  name
                           domain=domain,
                           use_ntlm_v2=True)

connection.connect(ip=host)  # The IP of file server

connection.storeFile(service_name=service_name,  # It's the name of shared folder
                     path=path,
                     file_obj=file_obj)
connection.close()


于 2020-12-27T07:04:56.337 回答