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

如何向sambal gem传递-m参数以指定SMB协议版本完成连接

解决方案

方案1:固定添加SMB2参数的最小补丁

直接在你实例化Sambal::Client的代码前插入以下补丁代码,即可自动为所有sambal发起的smbclient请求加上-m SMB2参数:

module Sambal
  class Client
    private
    # 重写命令构造逻辑,追加SMB2协议参数
    def smbclient_commands
      [
        "smbclient",
        "//#{host}/#{share}",
        "-U", "#{domain}\\#{user}%#{password}",
        "-m", "SMB2",
        "-c", command_file.path
      ].compact
    end
  end
end

补丁添加完成后,你原来的实例化代码无需修改即可正常运行:

client = Sambal::Client.new(domain: 'CORP', host: '10.9.167.71', share: 'Dept', user: 'thinger', password: '--no-pass')

方案2:支持灵活指定协议版本的通用补丁

如果后续可能需要切换其他SMB协议版本,可以使用更通用的补丁,新增protocol初始化参数:

module Sambal
  class Client
    alias_method :original_init, :initialize

    def initialize(options = {})
      @protocol = options.delete(:protocol)
      original_init(options)
    end

    private
    def smbclient_commands
      base_cmd = [
        "smbclient",
        "//#{host}/#{share}",
        "-U", "#{domain}\\#{user}%#{password}",
        "-c", command_file.path
      ]
      base_cmd += ["-m", @protocol] if @protocol
      base_cmd.compact
    end
  end
end

使用时在初始化参数中指定协议即可:

client = Sambal::Client.new(
  domain: 'CORP',
  host: '10.9.167.71',
  share: 'Dept',
  user: 'thinger',
  password: '--no-pass',
  protocol: 'SMB2'
)

两种方案都属于运行时猴子补丁,无需修改sambal gem本身的源码,也不需要直接调用smbclient命令,只会在当前脚本生效,不会影响其他项目的sambal使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 03:45:02