java程序通过密钥方式使用JSch API访问SSH(转帖)
2011-05-09 20:44 297人阅读 评论(0) 收藏 举报
java程序通过密钥方式使用JSch API访问SSH
2010-05-20 14:51
上面已经验证了通过密钥方式访问SSH Server是可行的,并且给自己搭建了一个测试环境,下面就开始我最终的目的:java程序通过密钥访问。
1、工程引入jsch-0.1.42.jar,可以到http://www.jcraft.com/jsch/官方下载。
2、在官方的example中,有一个demo,类UserAuthPubKey,是使用密钥访问的,参考了下,我对其进行了修改,改为自动连接并使用SFTP协议显示当前路径,代码如下:
package Test.sftp;
import com.jcraft.jsch.*;
public class TestKeyAcc {
public static void main(String[] arg) {
String keyFile = "./id_rsa";
String user = "username";
String host = "127.0.0.1";
String passphrase = "111111";
int port = 22;
try {
JSch jsch = new JSch();
jsch.addIdentity(keyFile);
Session session = jsch.getSession(user, host, port);
// username and passphrase will be given via UserInfo interface.
UserInfo ui = new MyUserInfo(passphrase);
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
System.out.println(sftp.pwd());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
public static class MyUserInfo implements UserInfo {
private String passphrase = null;
public MyUserInfo(String passphrase) {
this.passphrase = passphrase;
}
public String getPassphrase() {
return passphrase;
}
public String getPassword() {
return null;
}
public boolean promptPassphrase(String s) {
return true;
}
public boolean promptPassword(String s) {
return true;
}
public boolean promptYesNo(String s) {
return true;
}
public void showMessage(String s) {
System.out.println(s);
}
}
}
运行后结果显示:
****USAGE WARNING****
This is a private computer system. This computer system, including all
related equipment, networks, and network devices (specifically including
Internet access) are provided only for authorized use. This computer system
may be monitored for all lawful purposes, including to ensure that its use
is authorized, for management of the system, to facilitate protection against
unauthorized access, and to verify security procedures, survivability, and
operational security. Monitoring includes active attacks by authorized entities
to test or verify the security of this system. During monitoring, information
may be examined, recorded, copied and used for authorized purposes. All
information, including personal information, placed or sent over this system
may be monitored.
Use of this computer system, authorized or unauthorized, constitutes consent
to monitoring of this system. Unauthorized use may subject you to criminal
prosecution. Evidence of unauthorized use collected during monitoring may be
used for administrative, criminal, or other adverse action. Use of this system
constitutes consent to monitoring for these purposes.
/cygdrive/d/opensshhome/username
ok,good,问题解决了,如果不是密钥方式,与普通FTP一样的用户名及密码访问又是怎样的呢,那就比较简单了
去掉
jsch.addIdentity(keyFile);
及
UserInfo ui = new MyUserInfo(passphrase);
session.setUserInfo(ui);
在Session sshSession = jsch.getSession(userStr, serverIp, port);下增加
sshSession.setPassword(passwordStr);
如果在生成私钥时没有使用密码,那又是怎样的呢?其实很简单,如果不需要密码访问,你提供了密码也是通过的( new MyUserInfo(passphrase);中密码不null或空),大概过程是,先看是否需要密码,如果不需要,那么就直接过去,所以即便设置了密码也没问题。
在使用该API进行密钥及非密钥访问SFTP时,感觉不是很惬意,试验了许久才通过。
以上文字但愿对后来者有所帮助
分享到:
相关推荐
主要介绍了详解Java使用Jsch与sftp服务器实现ssh免密登录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
JSCH api 源码 ssh sftp telnet 协议
标签:jcraft、jsch、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
java进行ssh协议连接的jar包,jsch-0.1.54.jar,使用这一个jar即可
基于jsch包实现远程服务器的sftp访问,并可远程发送命令执行,可以配合https://download.csdn.net/download/u011230766/10710755,实现密钥登录,类KeyGen用于生成非对称密钥
JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后...
jsch_APIchm.rar
JSch是Java Secure Channel的缩写。JSch是一个SSH2的纯Java实现。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到你自己的应用程序。
是使用java使用JSCH进行连接远程服务器的Demo,让更多刚刚接触的同学进行学习。
用java 实现 ssh的jar包 下载
ssh开源开发API-JSCH 通过Java创建ssh等服务器连接 官方网站http://www.jcraft.com/jsch/
jsch通过java代码ssh登录主机远程执行命令并返回结果。注意,这个是源代码,请自行编译后在使用。当然,也可以根据自己的需要更改源代码。
升级jsch包jsch-0.1.53.jar 解决ssh升级后jschexception:algorithm negotiation fail 等问题
JSch - Java实现的SFTP(文件上传详解篇)
在java里可以通过Ganymed SSH-2, Expect4j等实现ssh登录,由于Ganymed SSH-2是纯java实现,并且可以实现scp,sftp等,因此我们采用Ganymed SSH-2 1> 下载安装 从http://code.google.com/p/ganymed-ssh-2/ 下载...
jsch-nio, 通过SSH实现 Java nio 文件系统 jsch-nio注意,这个项目依赖于 jsch扩展插件项目JSch是利用JSch实现实现一个 nio 文件系统,以及它随之一起运行的所有方法。 到目前为止,有一个功能齐全的unix/linux ...
示例连接SSH Java 和 JSch 的 SSH 连接示例(Java 安全通道)
参考expect4j的交互式操作方式控制输入输出流实现批量执行多条shell命令同时支持切换用户输入密码提权执行命令输入密码等操作 有完整的测试用例,便于理解 模板化执行命令示例 [$]send[su - root] [Password:]...
SSH协议连接远程服务器的java实现,运行稳定,性能极佳
标签:jcraft、jsch、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...