{"id":570,"date":"2016-12-30T19:01:29","date_gmt":"2016-12-30T10:01:29","guid":{"rendered":"http:\/\/okamurax.com\/?p=570"},"modified":"2017-01-06T20:02:19","modified_gmt":"2017-01-06T11:02:19","slug":"ruby-netssh-%e6%8e%a5%e7%b6%9a%e5%ae%9f%e9%a8%93","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=570","title":{"rendered":"Ruby NET\/SSH NET\/SCP\u63a5\u7d9a\u5b9f\u9a13"},"content":{"rendered":"<h2>SSH\u63a5\u7d9a<\/h2>\n<pre class=\"lang:ruby decode:true\">#https:\/\/github.com\/net-ssh\/net-ssh\/blob\/master\/README.rdoc\r\n\r\n#coding: shift_jis\r\n#gem install net-ssh\r\n\r\nrequire 'rubygems'\r\nrequire 'net\/ssh'\r\n\r\nNet::SSH.start('192.168.xxx.xxx','user', :keys=&gt;'id.key', :passphrase=&gt;'', :port=&gt;'22') do |ssh|\r\n\t\r\n\t#\u30d6\u30ed\u30c3\u30af\u306f\u30e1\u30bd\u30c3\u30c9\u306b\u6e21\u3059\u69cb\u9020\u5316\u3055\u308c\u305f\u5f15\u6570\r\n\t\r\n\tchannel = ssh.open_channel do |ch|\r\n\t\t\r\n\t\tputs \"1\", ch.class # Net::SSH::Connection::Channel\r\n\t\tputs \"2\", ch # &lt;Net::SSH::Connection::Channel:xxx&gt;\r\n\t\t\r\n=begin\r\n\t\t\t\t\t\r\n\t\tchannel.request_pty do |ch,success|\r\n\t\t\r\n\t\t\t#\u30d6\u30ed\u30c3\u30af\u5f15\u6570\u306f\u540d\u524d\u306b\u95a2\u4fc2\u306a\u304f\u3001TrueClass,Channnel\u304c\u8fd4\u3063\u3066\u304d\u3066\u3044\u308b\u3002\r\n\t\t\t\r\n\t\t\traise 'could not obtain pty' if !success #pty\u306f\u7591\u4f3c\u7aef\u672b\r\n\t\t\t\r\n\t\t\tputs \"3\", success.class # TrueClass \r\n\t\t\tputs \"4\", success # True\r\n\t\t\tputs \"5\", ch.class # Net::SSH::Connection::Channel\r\n\t\t\tputs \"6\", ch # &lt;Net::SSH::Connection::Channel:xxx&gt;\r\n\t\t\t\r\n\t\tend\r\n\r\n=end\r\n\r\n=begin\r\n\t\tch.exec \"ls\" do |ch,success|\r\n\t\t\t\r\n\t\t\t#\u51e6\u7406\u306f\u8d70\u308b\u3051\u3069\u6a19\u6e96\u51fa\u529b\u3092\u6301\u3063\u3066\u3053\u306a\u3044\u3002\u4e0b\u8a18\u53c2\u7167channel.wait\u304c\u5fc5\u8981\u3002\r\n\r\n\t\t\traise 'failed to execute command' unless success\r\n\t\t\t\r\n\t\t\tputs \"7\", success.class # TrueClass \r\n\t\t\tputs \"8\", success # True\r\n\t\t\tputs \"9\", ch.class # Net::SSH::Connection::Channel\r\n\t\t\tputs \"10\", ch # &lt;Net::SSH::Connection::Channel:xxx&gt;\r\n\t\t\t\r\n\t\t\tch.on_data do |c, data|\r\n\t\t\t\tputs data\r\n\t\t\tend\r\n\r\n\t\tend\r\n\t\t\r\n=end\r\n\r\n=begin\r\n\r\n\t\t#\u3053\u306e\u66f8\u304d\u65b9\u3060\u3068\u6a19\u6e96\u51fa\u529b\u3092\u6301\u3063\u3066\u3053\u308c\u308b\u3002\r\n\t\tssh.exec!(\"ls\") do |ch,stream,data|\r\n\t\t\t\r\n\t\t\tputs \"11\", ch # TrueClass \r\n\t\t\tputs data if stream == :stdout\r\n\t\t\t\r\n\t\tend\r\n\t\r\n=end\r\n\r\n\t\tch.exec \"ls\" do |ch|\r\n\t\t#\u3053\u306ech.exec\u304c\u306a\u3044channel\u304cclose\u3055\u308c\u306a\u3044\r\n\t\t\r\n\t\t\tch.on_data { |c,data| puts data }\r\n\t\t\t#channel.wait\u3092\u5165\u308c\u3066\u30011,2,12,13\u3068\u6b63\u3057\u304f\u51e6\u7406\u304c\u6d41\u308c\u308c\u3070on_data\u304c\u53d6\u5f97\u3067\u304d\u308b\u3002\r\n\t\t\t\r\n\t\tend\r\n\t\t\r\n\t\tch.on_close { puts \"done\" }\r\n\r\n\tend\r\n\t\r\n\tchannel.wait #\u3053\u306e\u884c\u304c\u306a\u3044\u306812,13\u304c\u8868\u793a\u3055\u308c\u3066\u304b\u30891,2\u3068\u306a\u308b\u3002\r\n\t#channel.wait\u304c\u3042\u3063\u3066\u3082\u3001ch.exec\u304c\u306a\u3044\u3068\u51e6\u7406\u304c\u7d42\u308f\u3089\u306a\u3044\u3002\r\n\r\n\tputs \"12\", channel.class\r\n\tputs \"13\", channel\r\n\t\r\nend\r\n\r\n<\/pre>\n<pre class=\"lang:ruby decode:true\">#coding: utf-8\r\n\r\nrequire 'rubygems'\r\nrequire 'net\/ssh'\r\n\r\nNet::SSH.start('192.168.xxx.xxx','user', :keys=&gt;'id.key', :passphrase=&gt;'', :port=&gt;'22') do |ssh|\r\n\t\r\n\tchannel = ssh.open_channel do |ch|\r\n\t\t\r\n\t\tchannel.request_pty { |c,success| raise '' if !success }\r\n\t\t\r\n\t\tch.exec \"sudo pwd\" do |ch|\r\n\t\t\r\n\t\t\tch.on_data do |ch,data|\r\n\t\t\t\t\r\n\t\t\t\tif data =~ \/sudo\/\r\n\t\t\t\t\t\r\n\t\t\t\t\tch.send_data \"xxx\" #\u3053\u306eif\u304c\u306a\u3044\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u4f55\u5ea6\u3082\u9001\u4fe1\u3057\u3066\u3044\u308b\u3002\r\n\r\n\t\t\t\telsif data =~ \/\\w+\/\r\n\t\r\n\t\t\t\t\tputs data\r\n\t\t\t\t\r\n\t\t\t\tend\r\n\r\n\t\t\tend\r\n\t\t\t\r\n\t\tend\r\n\t\t\r\n\t\tch.on_close { puts \"done\" }\r\n\r\n\tend\r\n\t\r\n\tchannel.wait\r\n\t\r\nend\r\n<\/pre>\n<h2>SCP\u63a5\u7d9a<\/h2>\n<pre class=\"lang:ruby decode:true\"># coding: utf-8\r\n\r\n# sudo gem install net-scp\r\n# sudo gem install net-ssh\r\n\r\nrequire 'rubygems'\r\nrequire 'net\/ssh'\r\nrequire 'net\/scp'\r\n\r\nNet::SSH.start('xxx.com','user',:keys=&gt;'id.key',:passphrase=&gt;'pass',:port=&gt;'22') do |ssh|\r\n\r\n    ssh.exec 'mysqldump -u db_user -pXXX -h xxx.com db &gt; test.sql'\r\n    ssh.loop\r\n    ssh.scp.download! \".\/test.sql\",\".\/test.sql\"\r\n    ssh.loop\r\n    ssh.exec 'rm test.sql'\r\n    ssh.loop\r\n\r\nend\r\n<\/pre>\n<p>\u30ec\u30f3\u30b5\u30d0\u3067\u904b\u7528\u3057\u3066\u3044\u308bMySQL\u306e\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u3092\u30ed\u30fc\u30ab\u30eb\u306eUbuntu\u3078\u4fdd\u5b58\u3002<br \/>\n\u5fc5\u8981\u3067\u3042\u308c\u3070cron\u306b\u767b\u9332\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SSH\u63a5\u7d9a #https:\/\/github.com\/net-ssh\/net-ssh\/blob\/master\/README.rdoc #coding: shift_jis #gem install net-ssh requ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=570\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Ruby NET\/SSH NET\/SCP\u63a5\u7d9a\u5b9f\u9a13&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=570"}],"version-history":[{"count":5,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":672,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/570\/revisions\/672"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}