Ruby读Samba分享

发布于 2024-12-09 11:38:05 字数 86 浏览 0 评论 0原文

我正在寻找一种从 Samba 共享中读取内容的方法。我想像 Dir 类一样使用它,例如打开和读取目​​录。这在 Ruby 中可能吗?

I'm looking for a way to read from a samba share. I want to use it like the Dir class, for example open and read of directories. Is this possible in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过去的过去 2024-12-16 11:38:05

如果您的主机操作系统是 Posix(类 UNIX)系统(不确定 Windows...),那么 Sambala gem 应该适合您:

gem install sambala

只要您的主机操作系统在 $PATH 中的某处有一个可用的 smbclient 可执行文件,这个 gem 就可以工作。只要您使用 Mac OS X、Linux 或其他一些 UNIX 变体,您就应该能够从终端运行以下命令来查看路径中是否有 smbclient:

which smbclient

如果您没有得到结果,请在 google 上搜索如何获取适用于您当前主机操作系统的 smbclient。如果您使用的是 Mac OS X,则只需安装 MacPorts,然后从终端运行以下命令即可安装 smbclient:

sudo port install samba3

The Sambala gem should work for you if your host OS is a Posix (UNIX-like) system (not sure about Windows...):

gem install sambala

This gem will work as long as your host OS has a working smbclient executable somewhere in your $PATH. As long as you're using Mac OS X, Linux, or some other UNIX variant, you should be able to run the following command from the terminal to see if you've got smbclient somewhere in your path:

which smbclient

If you don't get a result, do a google search on how to obtain smbclient for your current host OS. If you're on Mac OS X, you can simply install MacPorts and then run the following command from the terminal to get smbclient installed:

sudo port install samba3
半城柳色半声笛 2024-12-16 11:38:05

我想你可以尝试检查我的 gem,我已经开始它了,因为我对 sambala 也有同样的问题

https:// github.com/revilo/rsmbclient

I think you could try to check my gem, i've started it cause i've same issues with sambala

https://github.com/reivilo/rsmbclient

如梦亦如幻 2024-12-16 11:38:05

Sambala gem 将适用于 Posix(类 UNIX)系统上的 1.8.x Ruby 实现

gem install sambala

对于 Posix 上的 1.9.x Ruby,请使用 GLSIGNAL 的 fork。

git clone https://github.com/glsignal/sambala.git
cd sambala
gem build samabala
gem install ./sambala.gem

注意:GLSignal 的 gem 使用丰富的 github 源,该源已修补以在 1.9.x 上运行

正如 lottscarson 所说,只要您的主机操作系统在您的 $PATH 中的某处有一个可用的 smbclient 可执行文件,这些 gem 就可以工作。只要您使用 Mac OS X、Linux 或其他 Posix 变体,您应该能够从终端运行以下命令来查看路径中是否有 smbclient:

which smbclient

如果您没有得到结果,请在 google 上搜索如何获取适用于您当前主机操作系统的 smbclient。 (示例)

(RHEL/CENTOS/etc) yum install samba

(ubuntu/debian) sudo apt-get install samba smbfs

(osx) brew install samba

作为 smbclient 包装器的替代方案,您可以使用名为“net-smb”的 C 扩展 ruby​​ gem。这需要本机编译,并且不是纯 Ruby 编写的。

gem install net-smb

这还需要一些东西。

Ruby 1.9.3+
Samba 3.5+ (libsmbclient)
C compiler

安装示例

sudo apt-get install libsmbclient libsmbclient-dev

The Sambala gem will work for 1.8.x Ruby implementations on a Posix (UNIX-like) system

gem install sambala

For 1.9.x Ruby on Posix use GLSIGNAL's fork.

git clone https://github.com/glsignal/sambala.git
cd sambala
gem build samabala
gem install ./sambala.gem

Note: GLSignal's gem uses a github source of abundance that is patched to run on 1.9.x

As stated by lottscarson, these gems will work as long as your host OS has a working smbclient executable somewhere in your $PATH. As long as you're using Mac OS X, Linux, or some other Posix variant, you should be able to run the following command from the terminal to see if you've got smbclient somewhere in your path:

which smbclient

If you don't get a result, do a google search on how to obtain smbclient for your current host OS. (examples)

(RHEL/CENTOS/etc) yum install samba

(ubuntu/debian) sudo apt-get install samba smbfs

(osx) brew install samba

As an alternative to a wrapper for smbclient you could use a C extension ruby gem called 'net-smb'. This requires native compilation, and is not written pure ruby.

gem install net-smb

This requires a few things as well.

Ruby 1.9.3+
Samba 3.5+ (libsmbclient)
C compiler

Installation examples

sudo apt-get install libsmbclient libsmbclient-dev
ˉ厌 2024-12-16 11:38:05

Ruby_SMB 是 SMB 协议系列的原生 Ruby 实现。

自述文件

  sock = TCPSocket.new address, 445
  dispatcher = RubySMB::Dispatcher::Socket.new(sock)

  client = RubySMB::Client.new(dispatcher, username: 'msfadmin', password: 'msfadmin')
  client.negotiate
  client.authenticate

  begin
    tree = client.tree_connect('TEST_SHARE')
    puts "Connected to #{path} successfully!"
  rescue StandardError => e
    puts "Failed to connect to #{path}: #{e.message}"
  end

  files = tree.list(directory: 'subdir1')

  files.each do |file|
    create_time = file.create_time.to_datetime.to_s
    access_time = file.last_access.to_datetime.to_s
    change_time = file.last_change.to_datetime.to_s
    file_name   = file.file_name.encode("UTF-8")

    puts "FILE: #{file_name}\n\tSIZE(BYTES):#{file.end_of_file}\n\tSIZE_ON_DISK(BYTES):#{file.allocation_size}\n\tCREATED:#{create_time}\n\tACCESSED:#{access_time}\n\tCHANGED:#{change_time}\n\n"
  end

Ruby_SMB is a native Ruby implementation of the SMB Protocol Family.

From the README:

  sock = TCPSocket.new address, 445
  dispatcher = RubySMB::Dispatcher::Socket.new(sock)

  client = RubySMB::Client.new(dispatcher, username: 'msfadmin', password: 'msfadmin')
  client.negotiate
  client.authenticate

  begin
    tree = client.tree_connect('TEST_SHARE')
    puts "Connected to #{path} successfully!"
  rescue StandardError => e
    puts "Failed to connect to #{path}: #{e.message}"
  end

  files = tree.list(directory: 'subdir1')

  files.each do |file|
    create_time = file.create_time.to_datetime.to_s
    access_time = file.last_access.to_datetime.to_s
    change_time = file.last_change.to_datetime.to_s
    file_name   = file.file_name.encode("UTF-8")

    puts "FILE: #{file_name}\n\tSIZE(BYTES):#{file.end_of_file}\n\tSIZE_ON_DISK(BYTES):#{file.allocation_size}\n\tCREATED:#{create_time}\n\tACCESSED:#{access_time}\n\tCHANGED:#{change_time}\n\n"
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文