You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用IP地址通过pysmb连接Windows SMB共享失败问题咨询

pysmb连接Windows共享驱动器:IP地址无法连接的原因及解决方法

问题原因

pysmb的SMBConnection构造函数中,第四个参数(官方文档名为remote_name)需要传入目标SMB服务器的NetBIOS名称,而非IP地址。

SMB协议(尤其是启用NTLMv2验证时)依赖服务器的NetBIOS名称生成身份验证令牌,直接传入IP地址会导致验证流程失败,最终抛出NotConnectedError。

而Windows系统处理UNC路径(如\\ip_address\\share_name)时,会自动通过IP查询服务器的NetBIOS名称,补充协议所需的验证信息,因此可以正常访问,但pysmb需要你显式提供该名称。

解决方法

方法1:已知服务器NetBIOS名称时

构造SMBConnection时传入服务器的NetBIOS名称,connect方法使用IP地址:

from smb.SMBConnection import SMBConnection

username = "username@domain.com"
password = "password"
client_machine_name = "any_name"
server_netbios_name = "server_20xx"  # 服务器的NetBIOS名称
ip_address = "10.10.xxx.xx"

# 构造函数传入NetBIOS名称,而非IP
conn = SMBConnection(username, password, client_machine_name, 
                     server_netbios_name, use_ntlm_v2=True)
# 连接时使用IP地址和端口
conn.connect(ip_address, 139)

方法2:未知服务器NetBIOS名称时

通过NetBIOS类查询IP对应的服务器NetBIOS名称,再创建连接:

from smb.SMBConnection import SMBConnection
from smb.netbios import NetBIOS

username = "username@domain.com"
password = "password"
client_machine_name = "any_name"
ip_address = "10.10.xxx.xx"

# 查询服务器的NetBIOS名称
nb = NetBIOS()
server_netbios_names = nb.queryIPForName(ip_address)
nb.close()

if not server_netbios_names:
    raise ValueError("无法获取目标服务器的NetBIOS名称")
server_netbios_name = server_netbios_names[0]

# 使用获取到的NetBIOS名称创建连接
conn = SMBConnection(username, password, client_machine_name, 
                     server_netbios_name, use_ntlm_v2=True)
conn.connect(ip_address, 139)

内容的提问来源于stack exchange,提问作者Gary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:53:02