pysmb的例子

Mar*_*ova 9 python windows samba

你能给我一个使用pysmb库连接到某个samba服务器的例子吗?我已经阅读了类smb.SMBConnection.SMBConnection(用户名,密码,my_name,remote_name,domain ='',use_ntlm_v2 = True),但我无法弄清楚如何使用它

nei*_*eif 12

我最近一直在使用pysmb进行网络共享枚举,并发现找到好的/完整的例子并不容易.我会把你推荐给我用pysmb枚举smb共享的小脚本: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')
Run Code Online (Sandbox Code Playgroud)

  • 你救了我 我正在使用PySmbClient但是无法让它工作.谢谢. (2认同)

小智 7

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', '/')
Run Code Online (Sandbox Code Playgroud)

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

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

  • 谢谢.什么是client_machine_name和remote_machine_name变量甚至看起来像?我使用哪部分地址?在远程名称中包含"smb://"? (2认同)