JSch how to use with PuTTY private key

烈酒焚心 提交于 2019-12-19 04:19:08

问题


I'm trying to use JSch with a private key configuration. I've generated a public and private key using PuTTYgen but am unsure what to do with both of the files.

Which key (public/private) needs transferring to the server?


回答1:


  • First, you need to register your PuTTYgen-generated public key on the server. See Getting ready for public key authentication or (my) Set up SSH public key authentication.

  • And finally see Can we use JSch for SSH key-based communication? for details on using the private key in JSch.


Make sure you use the latest version of JSch, as older versions do not support the .ppk format natively.




回答2:


Code Snippet for connection using PuTTy private Key (.ppk)

JSch jsch=new JSch();
jsch.setKnownHosts("~\.ssh\know_hosts");
jsch.addIdentity("~\sshkey.ppk");
Session session=jsch.getSession("ec2-user", "54.12.11.90", 22);
session.setConfig("PreferredAuthentications", "publickey");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);

Have used 0.1.54 version of Jsch

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>


来源:https://stackoverflow.com/questions/28442505/jsch-how-to-use-with-putty-private-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!