rsmbclient 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rsmbclient.rb +126 -0
- metadata +45 -0
data/lib/rsmbclient.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Author:: od (mailto:od@idfuze.com)
|
2
|
+
# Copyright:: 2011 IDFUZE.COM Olivier DIRRENBERGER - Released under the terms of the MIT license
|
3
|
+
#
|
4
|
+
# :title:Rsmbclient
|
5
|
+
|
6
|
+
class Rsmbclient
|
7
|
+
|
8
|
+
require 'logger'
|
9
|
+
require 'pty'
|
10
|
+
require 'expect'
|
11
|
+
require 'time'
|
12
|
+
|
13
|
+
attr_reader :files, :dirs, :path, :connected
|
14
|
+
|
15
|
+
def initialize(options={:domain => '', :host => '', :share => '', :user => '', :password => ''})
|
16
|
+
begin
|
17
|
+
@o, @i, @pid = PTY.spawn("smbclient //#{options[:host]}/#{options[:share]} #{options[:password]} -W #{options[:domain]} -U #{options[:user]}")
|
18
|
+
|
19
|
+
res = @o.expect(/^smb:.*\\>/, 10)[0]
|
20
|
+
@connected = case res
|
21
|
+
when /^put/
|
22
|
+
res['putting'].nil? ? false : true
|
23
|
+
else
|
24
|
+
if res['NT_STATUS']
|
25
|
+
false
|
26
|
+
elsif res['timed out'] || res['Server stopped']
|
27
|
+
false
|
28
|
+
else
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
unless @connected
|
34
|
+
close if @pid
|
35
|
+
exit(1)
|
36
|
+
else
|
37
|
+
@path = "."
|
38
|
+
ls
|
39
|
+
end
|
40
|
+
rescue
|
41
|
+
raise RuntimeError.exception("Unknown Process Failed!! (#{$!.to_s})")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def resume
|
46
|
+
puts "-------------\npath:-------------\n"
|
47
|
+
puts " #{@path}"
|
48
|
+
puts "-------------\ndirs:-------------\n"
|
49
|
+
@dirs.each do |d|
|
50
|
+
puts " #{d}"
|
51
|
+
end
|
52
|
+
puts "-------------\nfiles:-------------\n"
|
53
|
+
@files.each do |f|
|
54
|
+
puts " #{f[0]} (#{f[2]})"
|
55
|
+
end
|
56
|
+
return nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def ls(mask='')
|
60
|
+
parse_files(ask("ls"))
|
61
|
+
end
|
62
|
+
|
63
|
+
def cd(dir)
|
64
|
+
if @dirs.include?(dir)
|
65
|
+
if ask "cd #{dir}"
|
66
|
+
ls
|
67
|
+
make_path(dir)
|
68
|
+
return true
|
69
|
+
else
|
70
|
+
return false
|
71
|
+
end
|
72
|
+
else
|
73
|
+
return nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def close
|
78
|
+
ask("quit")
|
79
|
+
@connected = false
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def ask(cmd)
|
85
|
+
@i.printf("#{cmd}\n")
|
86
|
+
response = @o.expect(/^smb:.*\\>/, 10)[0]
|
87
|
+
if response.nil?
|
88
|
+
$stderr.puts "Failed to do #{cmd}"
|
89
|
+
exit(1)
|
90
|
+
else
|
91
|
+
return response
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def make_path(str)
|
96
|
+
str.split("/").each do |s|
|
97
|
+
ap = @path.split("/")
|
98
|
+
@path = case s
|
99
|
+
when ".." then ap.pop && ap.join("/")
|
100
|
+
when "." then @path
|
101
|
+
else ap.push(s).join("/")
|
102
|
+
end unless s == "."
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def get_files(str)
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
def parse_files(str)
|
111
|
+
@files = []
|
112
|
+
@dirs = []
|
113
|
+
str.each_line do |line|
|
114
|
+
if line =~ /\s+(\S+)\s+(\w+)\s+(\d+)\s+(.+)$/
|
115
|
+
if $2 == "D"
|
116
|
+
@dirs << $1
|
117
|
+
else
|
118
|
+
#name, type, size, date
|
119
|
+
@files << [$1, $2, $3, (Time.parse($4) rescue "!!#{$4}")]
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
return @files.size + @files.size
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rsmbclient
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Olivier DIRRENBERGER
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-30 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Access your smb share from ruby
|
15
|
+
email: od@idfuze.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/rsmbclient.rb
|
21
|
+
homepage: http://rubygems.org/gems/rsmbclient
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.6
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Ruby smbclient wrapper
|
45
|
+
test_files: []
|