java程序通过密钥方式使用JSch API访问SSH(转帖)
2011-05-09 20:44
609 查看
java程序通过密钥方式使用JSch API访问SSH
2010-05-20 14:51
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 API访问SSH(转帖)
- github使用(通过webstorm创建一个nodejs项目的本地git库,推送到github进行管理,(ssh密钥无用户密码方式))
- 使用JAVA代码通过SSH访问远程windows,获取磁盘信息
- 通过java使用ssh访问远程Linux
- SSH通过密钥对验证方式进行远程访问及控制
- SSH通过密钥对验证方式进行远程访问及控制
- 如何使用Java程序通过JDBC访问HDInsight Hive Server
- Java程序中不通过hadoop jar的方式访问hdfs
- JAVA系列课程讲座一:使用Socket通信实现网络通信程序(TCP方式)
- salesforce 零基础学习(五十五)java通过SOAP方式定时访问某个文件然后插入到sObject中
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- linux——ssh远程访问,密钥的生成以及公钥的使用,上传与下载
- 如何在linux环境下调试JAVA程序使用访问timesten:
- [编写高质量代码:改善java程序的151个建议]建议76 集合运算时使用更优雅的方式
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- Java通过JDBC访问数据库(两种方式)
- mac中github使用.ppk密钥通过ssh协议clone代码
- 通过编程的方式控制程序的内存使用量
- jsp使用jquery通过ajax方式访问servlet