基于Jsch证书的身份验证和登录验证取消

时间:2013-01-08 20:12:48

标签: authentication ssh jsch

Jsch,private.ppk登录。

目前我有以下代码用于ssh登录但由于没有提供密钥而获得异常。

以下是我的错误

om.jcraft.jsch.JSchException: Auth cancel


 JSch jsch = new JSch();
        Session session = jsch.getSession(user_name, host, 22);


        UserInfo ui = new SSHUserInfo(password, true);
        session.setUserInfo(ui);
        //connect to remove server
        session.connect();

        //sudo login bamboo
        if (null != session && session.isConnected()) {
            session.disconnect();
        }

2 个答案:

答案 0 :(得分:2)

JSch jsch = new JSch();

// Here privateKey is a file path like "/home/me/.ssh/secret_rsa "
// passphrase is passed as a string like "mysecr"
jsch.addIdentity(privateKey, passphrase);

session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no"); 
// Or yes, up to you. If yes, JSch locks to the server identity so it cannot
// be swapped by another with the same IP.

session.connect();
channel = session.openChannel("shell");
out = channel.getOutputStream();
channel.connect();

答案 1 :(得分:0)

文件后缀“.ppk”表示您尝试使用Putty的pridate键,我猜。

自0.1.49以来,JSch一直支持Putty的私钥, 如果你的密钥是加密的,你必须在你的环境中安装“Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy Files”[1]。

然后,如果您经常使用Pageant,您可能有兴趣尝试使用jsch-agent-proxy [2]。

[1] http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
[2] https://github.com/ymnk/jsch-agent-proxy
相关问题