Example of pysmb

前端 未结 3 700
醉梦人生
醉梦人生 2021-02-13 23:57

Could you give me an example of using pysmb library to connect to some samba server? I\'ve read there\'s class smb.SMBConnection.SMBConnection(username, password, my_name, remot

3条回答
  •  生来不讨喜
    2021-02-14 00:24

    I have been using pysmb for network shares enumeration lately, and found that it is not so easy to find good / complete examples. I'd refer you to a small script that I wrote for enumerating smb shares with pysmb: https://github.com/n3if/scripts/tree/master/smb_enumerator

    For the sake of completeness, also, I post here the code snippet that accomplishes the connection and enumeration:

    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')
    

提交回复
热议问题