【问题标题】:Example of pysmbpysmb 的例子
【发布时间】:2012-05-02 04:34:27
【问题描述】:

你能给我一个使用 pysmb 库连接到一些 samba 服务器的例子吗? 我读过有 类 smb.SMBConnection.SMBConnection(用户名、密码、my_name、remote_name、domain=''、use_ntlm_v2=True) 但是不知道怎么用

【问题讨论】:

    标签: python windows samba


    【解决方案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()
    
    
    

    【讨论】:

      【解决方案2】:

      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 实例的列表。

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

      【讨论】:

      • 谢谢。 client_machine_name 和 remote_machine_name 变量甚至应该是什么样子?我使用地址的哪一部分?我要在远程名称中包含“smb://”吗?
      【解决方案3】:

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

      为了完整起见,我在这里贴出完成连接和枚举的代码sn-p:

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

      【讨论】:

      • 如果 samba 服务器有一个 'GUEST' 登录怎么办。在这种情况下,用户名和密码字段应该提供什么?
      • 我会说 User=GUEST 和 Password='' 但我应该试试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2012-10-10
      • 2011-01-24
      • 1970-01-01
      • 2016-10-23
      相关资源
      最近更新 更多