{"id":984,"date":"2014-10-10T19:29:27","date_gmt":"2014-10-10T11:29:27","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=984"},"modified":"2014-10-10T19:30:59","modified_gmt":"2014-10-10T11:30:59","slug":"%e4%bd%bf%e7%94%a8jcabi-ssh%e5%9c%a8java%e4%b8%ad%e6%93%8d%e4%bd%9cssh%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=984","title":{"rendered":"\u4f7f\u7528jcabi-ssh\u5728java\u4e2d\u64cd\u4f5cssh\u547d\u4ee4"},"content":{"rendered":"<h1>\u4f7f\u7528jcabi-ssh\u5728java\u4e2d\u64cd\u4f5cssh\u547d\u4ee4<\/h1>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u5728java\u4ee3\u7801\u4e2d\u8fdc\u7a0b\u8fde\u63a5ssh\uff0c\u5e76\u4e14\u6267\u884c\u4e00\u4e9bshell\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528jcabi-ssh\u8fd9\u4e2a\u5c0f\u6846\u67b6\uff0c\u7eafjava\u7f16\u5199\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u3002<\/p>\n<h2>\u4f9d\u8d56<\/h2>\n<p>java\u6846\u67b6\uff0c\u4f9d\u8d56\u7684\u5305\u80af\u5b9a\u662fjar\u6587\u4ef6\u4e86\uff0cjar\u5305\u5730\u5740<a href=\"http:\/\/repo1.maven.org\/maven2\/com\/jcabi\/jcabi-ssh\/1.1\/jcabi-ssh-1.1.jar\">http:\/\/repo1.maven.org\/maven2\/com\/jcabi\/jcabi-ssh\/1.1\/jcabi-ssh-1.1.jar<\/a>,\u5982\u679c\u4f7f\u7528maven\u7ba1\u7406\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4f9d\u8d56\uff1a<!--more--><\/p>\n<pre><code>    &lt;dependency&gt;\n        &lt;groupId&gt;com.jcabi&lt;\/groupId&gt;\n        &lt;artifactId&gt;jcabi-ssh&lt;\/artifactId&gt;\n        &lt;version&gt;1.1&lt;\/version&gt;\n    &lt;\/dependency&gt;<\/code><\/pre>\n<p>2.0\u7248\u672c\uff0c\u4f5c\u8005\u6ca1\u6709\u6b63\u5f0f\u53d1\u5e03<\/p>\n<pre><code>    &lt;repositories&gt;\n        &lt;repository&gt;\n            &lt;id&gt;oss.sonatype.org&lt;\/id&gt;\n            &lt;url&gt;https:\/\/oss.sonatype.org\/content\/repositories\/snapshots\/&lt;\/url&gt;\n        &lt;\/repository&gt;\n    &lt;\/repositories&gt;\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;com.jcabi&lt;\/groupId&gt;\n            &lt;artifactId&gt;jcabi-ssh&lt;\/artifactId&gt;\n            &lt;version&gt;2.0-SNAPSHOT&lt;\/version&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n<\/code><\/pre>\n<h2>\u4ee3\u7801\u793a\u4f8b<\/h2>\n<p>\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>    String hello = new Shell.Plain(\n        new SSH(\n            \"ssh.example.com\", 22,\n            \"yegor\", \"-----BEGIN RSA PRIVATE KEY-----...\")\n        ).exec(\"echo 'Hello, world!'\");\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u79c1\u94a5\u662f\u672c\u673a\u7684\u79c1\u94a5\u4e32\uff0clinux\u7684\u8bdd\u5728\/home\/yourname\/.ssh\u4e2d\u7684id_rsa\u6587\u4ef6\u4e2d\uff0c\u5c06\u91cc\u9762\u7684\u5185\u5bb9\u5168\u90e8copy\u5230\u65b9\u6cd5\u4e2d\uff0c\u5305\u62ec<strong>-----BEGIN RSA PRIVATE KEY-----<\/strong>,<strong>-----END RSA PRIVATE KEY-----<\/strong>\u8fd9\u4e24\u884c\u3002<\/p>\n<p>\u60f3\u5b9e\u73b0\u672c\u673a\u79c1\u94a5\u767b\u5f55\uff0c\u8fd8\u5f97\u628a\u672c\u673a\u7684\u516c\u94a5\u590d\u5236\u5230\u76ee\u6807\u673a\u5668\u4e0a\uff0c\u516c\u94a5\u5728.ssh\u6587\u4ef6\u5939\u4e2d\u7684id_rsa.pub\u6587\u4ef6\u4e2d\uff0c\u5c06\u91cc\u9762\u7684\u6587\u672c\u5168\u90e8\u590d\u5236\u5230\u76ee\u6807\u673a\u5668\/home\/yourname\/.ssh\/authorized_keys\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u672c\u673a\u514d\u5bc6\u7801ssh\u767b\u5f55\u76ee\u6807\u673a\u5668\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e2a\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f7f\u7528ssh\u4e0a\u4f20\u4e00\u4e2a\u6587\u4ef6\u5230\u670d\u52a1\u5668\uff0c\u7136\u540egrep\u5176\u4e2d\u7684\u5185\u5bb9\u6765\u663e\u793a\u3002<\/p>\n<pre><code>    Shell shell = new SSH(\n        \"ssh.example.com\", 22,\n        \"yegor\", \"-----BEGIN RSA PRIVATE KEY-----...\"\n        );\n    File file = new File(\"\/tmp\/data.txt\");\n    new Shell.Safe(shell).exec(\n        \"cat &gt; d.txt &amp;&amp; grep 'hello' d.txt\",\n        new FileInputStream(file),\n        Logger.stream(Level.INFO, this),\n        Logger.stream(Level.WARNING, this)\n    );\n<\/code><\/pre>\n<p><strong><code>SSH<\/code><\/strong>\u8fd9\u4e2a\u7c7b\u5b9e\u73b0\u4e86<code>Shell<\/code>\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u5176\u5b9e\u5c31\u4e00\u4e2a\u65b9\u6cd5<code>exec<\/code>,exec\u65b9\u6cd5\u63a5\u6536\u56db\u4e2a\u53c2\u6570:<\/p>\n<pre><code>    interface Shell {\n        int exec(\n            String cmd, InputStream stdin,\n            OutputStream stdout, OutputStream stderr\n        );\n    }\n<\/code><\/pre>\n<h3>Shell.Safe<\/h3>\n<p><code>Shell.Safe<\/code>\u5bf9\u539f\u751f\u7684Shell\u5bf9\u8c61\u505a\u4e86\u4e00\u4e9b\u5c01\u88c5\uff0c\u5982\u679c<code>exec<\/code>\u65b9\u6cd5\u8fd4\u56de\u503c\u4e0d\u662f0\uff0c\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u6839\u636e\u8fd4\u56de\u503c\u53ef\u4ee5\u786e\u8ba4shell\u547d\u4ee4\u662f\u5426\u6210\u529f\u6267\u884c\u3002<\/p>\n<pre><code>    Shell ssh = new Shell.Safe(\n        new SSH(\n            \"ssh.example.com\", 22,\n            \"yegor\", \"-----BEGIN RSA PRIVATE KEY-----...\"\n        )\n    );\n<\/code><\/pre>\n<h3>Shell.Verbose<\/h3>\n<p><code>Shell.Verbose<\/code>\u4e5f\u662f\u5c01\u88c5\u4e86<code>Shell<\/code>\uff0c\u5e76\u4e14\u628a<strong>stdout<\/strong>\u548c<strong>stderr<\/strong>\u590d\u5236\u5230slf4j\u65e5\u5fd7\u6d41\u4e2d\uff08jcabi-log\uff09\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code>    Shell ssh = new Shell.Verbose(\n        new Shell.Safe(\n            new SSH(\n            \"ssh.example.com\", 22,\n            \"yegor\", \"-----BEGIN RSA PRIVATE KEY-----...\"\n            )\n        )\n        );\n<\/code><\/pre>\n<h3>Shell.Plain<\/h3>\n<p><code>Shell.Plain<\/code>\u4e5f\u662f\u5c01\u88c5\u4e86<code>Shell<\/code>\uff0c\u5e76\u4e14\u91cd\u5199\u4e86<code>exec<\/code>\u65b9\u6cd5\uff0c\u5b83\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff08\u8981\u6267\u884c\u7684shell\u547d\u4ee4\uff09\uff0c\u5e76\u4e14\u5b83\u4e0d\u8fd4\u56deshell\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\uff080\u6216\u8005\u5176\u4ed6\uff09\uff0c\u800c\u662f\u5c06<code>stdout<\/code>\u8f93\u51fa\u51fa\u6765\u3002\u5f53\u6211\u4eec\u60f3\u67e5\u770b\u67d0\u6761\u547d\u4ee4\u7684\u8f93\u51fa\u5185\u5bb9\u65f6\uff0cShell.Plain\u5c31\u5f88\u65b9\u4fbf\u4e86\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff1a<\/p>\n<pre><code>    String login = new Shell.Plain(new Shell.Safe(ssh)).exec(\"whoami\");\n<\/code><\/pre>\n<p>\u5f53\u7136\u8fd8\u6709\u5176\u4ed6\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u4e4b\u524d\u5728\u6dd8\u6d4b\u8bd5\u4e2d\u770b\u5230\u6dd8\u5b9d\u81ea\u5df1\u5c01\u88c5\u4e86Orion-ssh2\u5f62\u6210\u81ea\u5df1\u7684\u5de5\u5177\uff0c\u53ebRCT\uff0c\u5b83\u652f\u6301\u7528\u6237\u540d\u3001\u5bc6\u7801\u767b\u9646\u3002\u5177\u4f53<a href=\"http:\/\/www.taobaotest.com\/blogs\/2542\">http:\/\/www.taobaotest.com\/blogs\/2542<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528jcabi-ssh\u5728java\u4e2d\u64cd\u4f5cssh\u547d\u4ee4 \u5982\u679c\u6211\u4eec\u60f3\u5728java\u4ee3\u7801\u4e2d\u8fdc\u7a0b\u8fde\u63a5ssh\uff0c\u5e76\u4e14\u6267\u884c\u4e00\u4e9bshell\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528jcabi-ssh\u8fd9\u4e2a\u5c0f\u6846\u67b6\uff0c\u7eafjava\u7f16\u5199\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,11,7,5],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-linux","tag-7","tag-5"],"views":5838,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=984"}],"version-history":[{"count":2,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}