{"id":584,"date":"2013-11-20T17:54:57","date_gmt":"2013-11-20T09:54:57","guid":{"rendered":"http:\/\/coderbee.net\/?p=584"},"modified":"2019-01-06T21:24:42","modified_gmt":"2019-01-06T13:24:42","slug":"sftp-auth-fail","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20131120\/584","title":{"rendered":"sftp auth fail"},"content":{"rendered":"<p>\u4eca\u5929\u8981\u505a\u4e00\u4e2a\u529f\u80fd\uff1a\u7528sftp\u534f\u8bae\u4f20\u6587\u4ef6\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u662f\u522b\u4eba\u914d\u7f6e\u7684\uff0c\u7ed9\u51fa\u8d26\u53f7\u5bc6\u7801\u548c\u7aef\u53e32888\u3002\u7528\u7684\u5e93\u662fjsch\uff0c\u7248\u672c0.1.50\u3002<\/p>\n<p>\u6839\u636ejsch\u7684\u793a\u4f8b\uff0c\u5f88\u5feb\u5c31\u6539\u51fa\u4e00\u4e2a\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"java\">import com.jcraft.jsch.Channel;\r\nimport com.jcraft.jsch.ChannelSftp;\r\nimport com.jcraft.jsch.JSch;\r\nimport com.jcraft.jsch.JSchException;\r\nimport com.jcraft.jsch.Session;\r\nimport com.jcraft.jsch.SftpException;\r\nimport com.jcraft.jsch.UserInfo;\r\n\r\npublic class Sftp {\r\n       private static Session getSession(String user, String passwd, String host,\r\n                   int port) throws JSchException {\r\n            UserInfo ui = new MyUserInfo(passwd);\r\n            JSch jsch = new JSch();\r\n            Session session = jsch.getSession(user, host, port);\r\n            session.setUserInfo(ui);\r\n            session.connect();\r\n\r\n             return session;\r\n      }\r\n\r\n       private static ChannelSftp getFtpChannel(Session session)\r\n                   throws JSchException {\r\n            Channel channel = session.openChannel( \"sftp\" );\r\n            channel.connect();\r\n            ChannelSftp c = (ChannelSftp) channel;\r\n\r\n             return c;\r\n      }\r\n\r\n       public static boolean putFile(String user, String passwd, String host,\r\n                   int port, String localeFile, String remoteFile) {\r\n            Session session = null ;\r\n            ChannelSftp channel = null ;\r\n             try {\r\n                  session = getSession(user, passwd, host, port);\r\n                  channel = getFtpChannel(session);\r\n                  channel.put(localeFile, remoteFile, ChannelSftp. OVERWRITE);\r\n                  channel.quit();\r\n            } catch (JSchException e) {\r\n                  e.printStackTrace();\r\n                   return false ;\r\n            } catch (SftpException e) {\r\n                  e.printStackTrace();\r\n                   return false ;\r\n            } finally {\r\n                   clear(session, channel);\r\n            }\r\n             return true ;\r\n      }\r\n\r\n       private static void clear(Session session, Channel channel) {\r\n             if (channel != null) {\r\n                  channel.disconnect();\r\n            }\r\n             if (session != null) {\r\n                  session.disconnect();\r\n            }\r\n      }\r\n\r\n       public static void main(String[] arg) {\r\n             boolean b = Sftp.putFile( \"user\" , \"passwd\" , \"host\" , 2888, \"localeFile\" ,\r\n                         \"remoteFile\" );\r\n            System. out .println(b);\r\n      }\r\n\r\n       public static class MyUserInfo implements UserInfo {\r\n             final String passwd ;\r\n\r\n             public MyUserInfo(String passwd) {\r\n                   this .passwd = passwd;\r\n            }\r\n\r\n             public String getPassword() {\r\n                   return passwd ;\r\n            }\r\n\r\n             public boolean promptYesNo(String str) {\r\n                   \/\/ step 1, confirm accept host\r\n                   return true ; \/\/ \u63a5\u53d7\u4efb\u610f\u670d\u52a1\u5668\u7684\u6307\u7eb9\r\n            }\r\n\r\n             public String getPassphrase() {\r\n                   return null ;\r\n            }\r\n\r\n             public boolean promptPassphrase(String message) {\r\n                   return true ;\r\n            }\r\n\r\n             public boolean promptPassword(String message) {\r\n                   \/\/ step 2\r\n                   return true ;\r\n            }\r\n\r\n             public void showMessage(String message) {\r\n            }\r\n      }\r\n}\r\n<\/code><\/pre>\n<p>\u62ff\u81ea\u5df1\u7684VPS linux\u6d4b\u8bd5\u4e0b\uff0c\u4e0a\u4f20\u662fOK\u7684\u3002\u4f46\u662f\u4e0a\u4f20\u5230\u6d4b\u8bd5\u670d\u52a1\u5668\u65f6\u5374\u8001\u662f\u51fa\u9519\uff0c\u5728\u5efa\u7acb\u4f1a\u8bdd\u65f6\u603b\u662f\u629b\u51fa\u5f02\u5e38\uff1a<code>com.jcraft.jsch.JSchException : Auth fail<\/code>\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\u6000\u7591\u662f\u8d26\u53f7\u6709\u95ee\u9898\uff0c\u4f46\u5728Xftp\u4e0b\u662f\u53ef\u4ee5\u767b\u5f55\u7684\u3002\u5728shell\u7684sftp\u547d\u4ee4\u4e0b\u663e\u793a\u6307\u5b9a<code>oPort<\/code>\u548c<code>oPasswordAuthentication<\/code>\u9009\u9879\u4e5f\u53ef\u4ee5\u767b\u5f55\uff0c\u6240\u4ee5\u5c31\u4e0d\u50cf\u662f\u8d26\u53f7\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u65e0\u5948\u7ee7\u7eedgoogle\uff0c\u53d1\u73b0\u5f88\u591a\u7f51\u4e0a\u4ee3\u7801\u90fd\u6307\u5b9a<code>StrictHostKeyChecking<\/code>\u9009\u9879\u4e3a<code>no<\/code>\uff0c\u5c31\u628a\u4ee3\u7801\u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"java\">     private static Session getSession(String user, String passwd, String host,\r\n            int port) throws JSchException {\r\n        JSch jsch = new JSch();\r\n        Session session = jsch.getSession(user, host, port);\r\n        session.setConfig( \"StrictHostKeyChecking\" , \"no\" ); \/\/ \u4e0d\u9a8c\u8bc1host-key\uff0c\u9a8c\u8bc1\u4f1a\u5931\u8d25\u3002\r\n        session.setPassword(passwd);\r\n        session.connect();\r\n\r\n        return session;\r\n    }\r\n<\/code><\/pre>\n<p>\u8fd8\u771f\u884c\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5176\u5b9e\u662f\u56e0\u4e3ajsch\u8fdb\u884c\u4e25\u683c\u7684 SSH \u516c\u94a5\u68c0\u67e5\u5bfc\u81f4\u7684\uff0c\u7981\u7528 SSH \u8fdc\u7a0b\u4e3b\u673a\u7684\u516c\u94a5\u68c0\u67e5\u53ef\u4ee5\u65b9\u4fbf\u8fdb\u884c\u81ea\u52a8\u5316\u4efb\u52a1\u6267\u884c\u3002\u5982\u679c\u662f\u5728shell\u547d\u4ee4\u884c\u4e0b\u8fdb\u884c\u7684\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u5efa\u8bae\u91c7\u7528\u5ba2\u6237\u7aef\u516c\u94a5\u8ba4\u8bc1\uff0c\u4e5f\u5c31\u662f<a href=\"http:\/\/coderbee.net\/index.php\/linux\/20130530\/162\">ssh\u81ea\u52a8\u767b\u5f55<\/a>\u7684\u65b9\u5f0f\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u8981\u505a\u4e00\u4e2a\u529f\u80fd\uff1a\u7528sftp\u534f\u8bae\u4f20\u6587\u4ef6\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u662f\u522b\u4eba\u914d\u7f6e\u7684\uff0c\u7ed9\u51fa\u8d26\u53f7 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20131120\/584\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[115,113,114],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/584"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":1754,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/584\/revisions\/1754"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}