1条回答 默认 最新
- save4me 2016-01-14 18:42关注
请尝试
首先确保服务器上的/etc/ssh/sshd_config中的PasswordAuthentication设置为
PasswordAuthentication yes
如果还不行,可以参考sftp auth fail或者SFTP连接异常 Could not connect to SFTP server. Caused by: com.jcraft.jsch.JSchException: Auth fail
发现很多网上代码都指定StrictHostKeyChecking选项为no,就把代码改为下面这样:
private static Session getSession(String user, String passwd, String host, int port) throws JSchException { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); session.setConfig( "StrictHostKeyChecking" , "no" ); // 不验证host-key,验证会失败。 session.setPassword(passwd); session.connect(); return session; }
这个问题其实是因为jsch进行严格的 SSH 公钥检查导致的,禁用 SSH 远程主机的公钥检查可以方便进行自动化任务执行。如果是在shell命令行下进行的自动化任务,建议采用客户端公钥认证,也就是ssh自动登录的方式。
解决评论 打赏 举报无用 9