Ruby ãã Windows ã®å ±æãã©ã«ãã«ã¢ã¯ã»ã¹ãã
Linux ä¸ã§ Samba ãã¦ã³ãããã« Windows ã®å ±æãã©ã«ãã«ã¢ã¯ã»ã¹ãããã£ãã®ã§ãRuby ã® sambal ã¨ããã©ã¤ãã©ãªã使ã£ã¦ã¿ã¾ããã
johnae/sambal
https://github.com/johnae/sambal
試ããç°å¢ã¯ä»¥ä¸ã§ãã
Ruby ã 2.0 ãªã®ã¯åã«æå 㧠2.1 ã® RPM ãã¾ã ä½æãã¦ããªãã ãã§ã^^ï¼
ã¤ã³ã¹ãã¼ã«
sambal 㯠smbclient ã³ãã³ãã®ã©ããã¼ãªã®ã§ãåä½ã«ã¯ smbclient ã³ãã³ããå¿
è¦ã«ãªãã¾ãã
smbclient 㯠yum ãã samba-client ã³ãã³ããå®è¡ããã¨ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
# yum install -y samba-client
ã¤ã³ã¹ãã¼ã«ããã Windows ã®å ±æãã©ã«ãã«ã¢ã¯ã»ã¹ã§ãããã確èªã
$ smbclient //PC001/Share "shakiyamain" -U akishin -p 445
smb: \> ã¨ããããã³ããã表示ããã¦ããã°æ¥ç¶ã¯æåã§ãã
ls ã¨æã¤ã¨å
±æãã©ã«ãå
ã®ãã¡ã¤ã«ã確èªããäºãåºæ¥ã¾ãã
ããã³ãã㯠exit ã§æããäºãã§ãã¾ãã
smb: \> ls . D 0 Tue Feb 18 22:43:37 2014 .. D 0 Tue Feb 18 22:43:37 2014 test.txt A 0 Tue Feb 18 22:43:36 2014 62499 blocks of size 16777216. 33957 blocks available smb: \> exit
ã¡ãªã¿ã« WORKGROUP ã§ã¯ãªããã¡ã¤ã³ã«åå ãã¦ããå ´åã¯ä»¥ä¸ã®ãããªæãã
$ smbclient //PC001.example.co.jp/Share "p@ssword" -W example.co.jp -U akishin -p 445
ãã㧠sambal ã使ãæºåã¯æ´ãã¾ããã
ããã§ã¯ bundler ã§ã¤ã³ã¹ãã¼ã«ããã®ã§ãã¾ã㯠Gemfile ãä½æãã¾ãã
$ bundle init
å®è¡ãããã£ã¬ã¯ããªå ã« Gemfile ãçæãããã®ã§ç·¨éãã¾ãã
$ vi Gemfile
æ¬æ¥ãªãé å¸ããã¦ãã gem ã使ãã°ããã®ã§ãgem 'sambal'ãã¨æ¸ãã°ããã®ã§ããã試ããã¨ãã CentOS ã§ã¯ä»¥ä¸ã®ãããªã¨ã©ã¼ã«ãªã£ã¦ãã¾ãä¸æãåãã¾ããã
$ bundle exec ruby sambal_example.rb Failed to connect Unknown Process Failed!! (exit): "exit" /root/vendor/bundle/ruby/2.0.0/gems/sambal-0.1.2/lib/sambal.rb:70:in `exit' /root/vendor/bundle/ruby/2.0.0/gems/sambal-0.1.2/lib/sambal.rb:70:in `initialize' sambal_example.rb:9:in `new' sambal_example.rb:9:in `<main>'
ã©ãã smbclient ã³ãã³ãã®ããã³ããæååããã¼ã¹ãã¦ããæ£è¦è¡¨ç¾ãè¥å¹²ç°ãªãããã§ãã
ä»æ¹ãªãã®ã§åãããã Fork ãã¦ã¨ã©ã¼ã«ãªããªãããã«ä¿®æ£ãããã®ã使ç¨ãã¾ããã
https://github.com/akishin/sambal
ä¸è¨ã使ãå ´åã Gemfile ã«ã¯ä»¥ä¸ã®ããã«è¨è¿°ãã¾ãã
gem 'sambal', github: "akishin/sambal"
bundle install å®è¡ã
bundle install --path=vendor/bundle
ãã㧠sambal ã®ã¤ã³ã¹ãã¼ã«ã¯å®äºã§ãã
使ã£ã¦ã¿ã
以ä¸ã®ãããªæãã§æ¥ç¶ã㦠ls ãå®è¡ãã¦ã¿ã¾ãã
require 'sambal' begin HOST = 'PC001' DIR = 'Share' USER = 'akishin' PASS = 'p@ssword' client = Sambal::Client.new(host: HOST, share: DIR, user: USER, password: PASS) puts client.ls client.close rescue => e puts e end
ãã¡ã¤ã³ã«åå ãã¦ãã Windows ã«æ¥ç¶ããå ´åã¯ä»¥ä¸ã®ããã«ãã¦ãã¡ã¤ã³ãæå®ãã¾ãã
require 'sambal' begin DOMAIN = 'example.co.jp' HOST = 'PC001.example.co.jp' DIR = 'Share' USER = 'akishin' PASS = 'p@ssword' client = Sambal::Client.new(domain: DOMAIN, host: HOST, share: DIR, user: USER, password: PASS) puts client.ls client.close rescue => e puts e end
å®è¡ããã¨ä»¥ä¸ã®ãããªçµæã表示ããã¾ããã
$ bundle exec ruby sambal_example.rb {"."=>{:type=>:directory, :size=>"0", :modified=>2014-02-18 22:43:37 +0900}, ".."=>{:type=>:directory, :size=>"0", :modified=>2014-02-18 22:43:37 +0900}, "test.txt"=>{:type=>:file, :size=>"0", :modified=>2014-02-18 22:43:36 +0900}}
ã¡ããã¨å ±æãã©ã«ãã®å 容ãèªã¿åãã¦ããããã§ãã
次ã¯ä½ããã¡ã¤ã«ãç½®ãã¦ã¿ã¾ãã
Sambal::Client#put ã¡ã½ããã§ãã¼ã«ã«ã®ãã¡ã¤ã«ãå
±æãã©ã«ãã«é
ç½®ãããã¨ãã§ãã¾ãã
require 'sambal' begin HOST = 'PC001' DIR = 'Share' USER = 'akishin' PASS = 'p@ssword' client = Sambal::Client.new(host: HOST, share: DIR, user: USER, password: PASS) client.put('/home/akishin/image.png', 'image.png') # <- è¿½è¨ puts client.ls client.close rescue => e puts e end
å®è¡ããã¨ä»¥ä¸ã®ããã« image.png ãçµæã«å«ã¾ãã¦ããäºããããã¾ãã
ã¾ããå®éã«å
±æãã©ã«ãã確èªããã¨ãã¡ã¤ã«ãé
ç½®ããã¦ãã¾ããã
$ bundle exec ruby sambal_example.rb {"."=>{:type=>:directory, :size=>"0", :modified=>2014-02-18 23:06:46 +0900}, ".."=>{:type=>:directory, :size=>"0", :modified=>2014-02-18 23:06:46 +0900}, "image.png"=>{:type=>:file, :size=>"29618", :modified=>2014-02-18 23:06:46 +0900}, "test.txt"=>{:type=>:file, :size=>"0", :modified=>2014-02-18 22:43:36 +0900}}
å½ç¶ã§ãã Windows ã®å ±æãã©ã«ãã«æ¥ç¶ã¦ã¼ã¶ã§æ¸è¾¼ã¿æ¨©éãå¿ è¦ãªã®ã§ãããä¸æãè¡ããªãå ´åã«ã¯ç¢ºèªãã¦ã¿ã¦ä¸ããã
ã¨ããããã§ããããªæãã§ãããããã¦ã³ãããªãã¦ã Windows ã®å
±æãã©ã«ãã«ç°¡åã«ã¢ã¯ã»ã¹ããäºãåºæ¥ã¾ããã
Windows, Linux ãæ··å¨ãã¦ãããããªç°å¢ã§ã¹ã¯ãªãããæ¸ãéã«ã¯çµæ§éå®ãããã§ãã