Class: SMB::Client

Inherits:
Object
  • Object
show all
Includes:
ClientHelper
Defined in:
lib/smb/client/client.rb,
lib/smb/client/version.rb,
lib/smb/client/runtime_error.rb,
lib/smb/client/connection_error.rb

Overview

Low-level interface to smbclient executable

Defined Under Namespace

Classes: ConnectionError, RuntimeError

Constant Summary collapse

VERSION =
'0.1.6'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientHelper

#del, #exist?, #get, #ls, #mkdir, #put, #read, #rmdir, #write

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new instance and connects to server

Parameters:

  • options (Hash) (defaults to: {})

    Hash with connection options



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smb/client/client.rb', line 19

def initialize(options = {})
  @executable = ENV.fetch('SMBCLIENT_EXECUTABLE') { 'smbclient' }
  @options = {
    user: options[:user],
    share: options[:share],
    password: options[:password],
    version: options[:version] || 2,
    host: options[:host] || 'localhost',
    workgroup: options[:workgroup] || 'WORKGROUP'
  }

  Thread.abort_on_exception = true
  connect

  # Pipe used to pass commands to pty
  @read1, @write1 = IO.pipe

  # Pipe used to pass responses from pty
  @read2, @write2 = IO.pipe

  # Indicates if first output should be ignored
  @first_message = true

  @connection_established = false
  @shutdown_in_progress = false
end

Instance Attribute Details

#executableObject

Returns the value of attribute executable.



15
16
17
# File 'lib/smb/client/client.rb', line 15

def executable
  @executable
end

#pidObject

Returns the value of attribute pid.



15
16
17
# File 'lib/smb/client/client.rb', line 15

def pid
  @pid
end

Instance Method Details

#closeObject

Closes the connection to the server end terminates all running threads



47
48
49
50
# File 'lib/smb/client/client.rb', line 47

def close
  @shutdown_in_progress = true
  Process.kill('QUIT', @pid) == 1
end

#exec(cmd) ⇒ Object

Execute a smbclient command

Parameters:

  • cmd (String)

    The command to be executed

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smb/client/client.rb', line 54

def exec(cmd)
  # Send command
  @write1.puts cmd

  # Wait for response
  text = @read2.read

  # Close previous pipe
  @read2.close

  # Create new pipe
  @read2, @write2 = IO.pipe

  # Raise at the end to support continuing
  raise Client::RuntimeError, text if text.start_with? 'NT_STATUS_'

  text
end