简体   繁体   English

从 Docker 容器中运行的 Python 代码访问 Windows CIFS 共享

[英]Accessing a Windows CIFS share from Python code running in a Docker container

I'm trying to access multiple Windows CIFS shares from a Python code that will run in a Docker container.我正在尝试从将在 Docker 容器中运行的 Python 代码访问多个 Windows CIFS 共享。 I've seen that there are multiple SMB libraries like pysmb and smbprotocol that claim that they can be used to access CIFS shares, but I haven't managed to get it to work and haven't seen a single example online where they are used to access CIFS shares.我已经看到有多个 SMB 库(如pysmbsmbprotocol声称它们可用于访问 CIFS 共享,但我还没有设法让它工作,也没有在网上看到一个使用它们的示例访问 CIFS 共享。

I know that a solution would be to mount the share on the host and mount it to the container, but I'd rather to avoid that if possible, as the code will need to access multiple shares and not all will be known when the container starts.我知道一个解决方案是将共享挂载到主机上并将其挂载到容器中,但如果可能的话,我宁愿避免这种情况,因为代码需要访问多个共享,并且当容器时并非所有共享都被知道开始。

Am I missing something?我错过了什么吗? Is there a good way or a good example online on how to access CIFS share from Python code that runs on Linux?关于如何从在 Linux 上运行的 Python 代码访问 CIFS 共享的在线好方法或好示例? (I know that on Windows you can simply open the folder, but I need it to work on Linux). (我知道在 Windows 上你可以简单地打开文件夹,但我需要它在 Linux 上工作)。

What is known to not work is a call to mount inside the container unless the container was started with privileges .众所周知, 除非容器以特权启动,否则在容器内调用mount是行不通的。 However client code could also connect to CIFS drives without first mounting the directory (eg for Java it is jcifs-ng ).然而,客户端代码也可以连接到 CIFS 驱动器而无需先挂载目录(例如,对于 Java,它是jcifs-ng )。

Find out how the library you use works internally.了解您使用的库在内部是如何工作的。 If it can connect directly go on.如果可以直接连接 go 就可以了。 Otherwise you may also add smbclient to your container and call that to access files on the CIFS side.否则,您也可以将smbclient添加到容器中并调用它来访问 CIFS 端的文件。

There's no CIFS specific example because it should work the same as SMB in linux.没有 CIFS 特定示例,因为它应该与 linux 中的 SMB 一样工作。 I have been struggling for a while with both libraries ( pysmb and smbprotocol ) and I have made them work.我已经为这两个库( pysmbsmbprotocol )苦苦挣扎了一段时间,我已经让它们工作了。

For pysmb , the only thing I've found different is that examples usually tell you can leave domain empty or don't tell it.对于pysmb ,我发现唯一不同的是示例通常告诉您可以将域留空或不告诉它。 On windows, it is the workgroup.在 windows 上,它是工作组。 If leaving it empty doesn't work, you can check it out by right-clicking on “My Computer” and selecting “Properties”.如果将其留空不起作用,您可以通过右键单击“我的电脑”并选择“属性”来检查它。 You'll find it there together with the machine name.您会在那里找到它以及机器名称。

Here's a working example with pysmb from linux to a windows machine:这是从 linux 到 windows 机器的pysmb的工作示例:

from smb.SMBConnection import SMBConnection
conn = SMBConnection(username="user", password="my_passwrd", my_name="name", remote_name="WINDOWS-MACHINE", domain="WORKGROUP", use_ntlm_v2=True)
conn.connect(my_server_ip_addr)

for file in conn.listPath("shared_dir", "/"):
   print(file.filename)
conn.close()

You can check an example using smbprotocol here (where "server" is you IP address or host and "share" your shared directory name).您可以在此处查看使用smbprotocol的示例(其中“服务器”是您的 IP 地址或主机并“共享”您的共享目录名称)。 You can make it work without remote_name nor domain.您可以在没有 remote_name 或域的情况下使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM