Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pysmb示例_Python_Windows_Samba - Fatal编程技术网

Python pysmb示例

Python pysmb示例,python,windows,samba,Python,Windows,Samba,您能给我一个使用pysmb库连接到某个samba服务器的示例吗? 我已经读过了 类smb.SMBConnection.SMBConnection(用户名、密码、我的\u名称、远程\u名称、域=“”,使用\u ntlm\u v2=True) 但是我不知道如何使用它,SMBConnection类将允许您以阻塞模式访问远程Samba服务器上的文件 要检索远程服务器上共享文件夹中的文件列表 from smb.SMBConnection import SMBConnection conn = SMBCon

您能给我一个使用pysmb库连接到某个samba服务器的示例吗? 我已经读过了 类smb.SMBConnection.SMBConnection(用户名、密码、我的\u名称、远程\u名称、域=“”,使用\u ntlm\u v2=True)
但是我不知道如何使用它,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源程序包的
测试/SMBConnectionTests
文件夹中可以找到更多示例。

我最近一直在使用pysmb进行网络共享枚举,发现要找到好的/完整的示例并不容易。请参阅我为使用pysmb枚举smb共享而编写的一个小脚本:

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

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

例如,您希望通过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()



如果samba服务器有一个“来宾”登录怎么办。在这种情况下,用户名和密码字段应该提供什么?我会说User=GUEST,password='',但我应该试试。谢谢。客户端\机器\名称和远程\机器\名称变量应该是什么样子?我要用哪个地址?我是否在远程名称中包含“smb://”?