Java使用JSch远程连接SSH,模拟sshClient
2014-05-20 11:50
453 查看
1. SSH简介
SSH是Secure Shell的缩写,一种建立在应用层和传输层基础上的安全协议。SSH在连接和传送过程中会加密所有数据,可以用来在不同系统或者服务器之间进行安全连接。SSH提供两种的安全验证方式:基于密码的认证和基于密匙的认证。其中,基于密码的认证比较简单,只要知道远程主机的用户名和密码,就可以进行登录。基于密匙的认证比较麻烦,而且连接比较耗时,这里不详细介绍。有很多基于SSH协议的客户端,例如:PuTTY、OpenSSH、Xshell
4等,可以远程连接几乎所有UNIX平台。同时,可以通过Linux命令行ssh uername@host连接到某主机。
在项目中,如何利用代码实现SSH,远程执行Shell脚本呢?JSch是Java Secure Channel的缩写,是一个SSH2功能的纯Java实现,具体信息可以参考JSch官网。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,同时你也可以集成它的功能到你自己的应用程序。在使用前,需要下载并导入JSch包:jsch-0.1.50.jar。
2. 实现原理
1. 根据远程主机的IP地址,用户名和端口,建立会话(Session);2. 设置用户信息(包括密码和Userinfo),然后连接session;
3. 在session上建立指定类型的通道(Channel),本文示例中采用ChannelExec类型的;
4. 设置channel上需要远程执行的Shell脚本,连接channel,就可以远程执行该Shell脚本;
5. 可以读取远程执行Shell脚本的输出,然后依次断开channel和session的连接。
3. 示例代码及分析
SSHCommandExecutor.java:[java] view
plaincopy
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* This class provide interface to execute command on remote Linux.
*/
public class SSHCommandExecutor {
private String ipAddress;
private String username;
private String password;
public static final int DEFAULT_SSH_PORT = 22;
private Vector<String> stdout;
public SSHCommandExecutor(final String ipAddress, final String username, final String password) {
this.ipAddress = ipAddress;
this.username = username;
this.password = password;
stdout = new Vector<String>();
}
public int execute(final String command) {
int returnCode = 0;
JSch jsch = new JSch();
MyUserInfo userInfo = new MyUserInfo();
try {
// Create and connect session.
Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
session.setPassword(password);
session.setUserInfo(userInfo);
session.connect();
// Create and connect channel.
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
BufferedReader input = new BufferedReader(new InputStreamReader(channel
.getInputStream()));
channel.connect();
System.out.println("The remote command is: " + command);
// Get the output of remote command.
String line;
while ((line = input.readLine()) != null) {
stdout.add(line);
}
input.close();
// Get the return code only after the channel is closed.
if (channel.isClosed()) {
returnCode = channel.getExitStatus();
}
// Disconnect the channel and session.
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return returnCode;
}
public Vector<String> getStandardOutput() {
return stdout;
}
public static void main(final String [] args) {
SSHCommandExecutor sshExecutor = new SSHCommandExecutor("xx.xx.xx.xx", "username", "password");
sshExecutor.execute("uname -s -r -v");
Vector<String> stdout = sshExecutor.getStandardOutput();
for (String str : stdout) {
System.out.println(str);
}
}
}
getSession()只是创建一个session,需要设置必要的认证信息之后,调用connect()才能建立连接。
调用openChannel(String type) 可以在session上打开指定类型的channel。该channel只是被初始化,使用前需要先调用connect()进行连接。
Channel的类型可以为如下类型:
shell - ChannelShell
exec - ChannelExec
direct-tcpip - ChannelDirectTCPIP
sftp - ChannelSftp
subsystem - ChannelSubsystem
其中,ChannelShell和ChannelExec比较类似,都可以作为执行Shell脚本的Channel类型。它们有一个比较重要的区别:ChannelShell可以看作是执行一个交互式的Shell,而ChannelExec是执行一个Shell脚本。
MyUserInfo:
[java] view
plaincopy
import com.jcraft.jsch.UserInfo;
/**
* This class provide interface to feedback information to the user.
*/
public class MyUserInfo implements UserInfo {
private String password;
private String passphrase;
@Override
public String getPassphrase() {
System.out.println("MyUserInfo.getPassphrase()");
return null;
}
@Override
public String getPassword() {
System.out.println("MyUserInfo.getPassword()");
return null;
}
@Override
public boolean promptPassphrase(final String arg0) {
System.out.println("MyUserInfo.promptPassphrase()");
System.out.println(arg0);
return false;
}
@Override
public boolean promptPassword(final String arg0) {
System.out.println("MyUserInfo.promptPassword()");
System.out.println(arg0);
return false;
}
@Override
public boolean promptYesNo(final String arg0) {
System.out.println("MyUserInfo.promptYesNo()");
System.out.println(arg0);
if (arg0.contains("The authenticity of host")) {
return true;
}
return false;
}
@Override
public void showMessage(final String arg0) {
System.out.println("MyUserInfo.showMessage()");
}
}
MyUserInfo实现了接口UserInfo,主要是为获得运行执行的用户信息提供接口。大部分实现方法中,没有做实质性的工作,只是输出一下trace信息,帮助判断哪个方法被执行过。
4. 执行结果分析
1. 如果不设置UserInfo,会抛出JSchException异常,提示找不到host:2. 如果MyUserInfo实现了接口UserInfo,但是只是@Override一些函数,会出现如下错误:
虽然可以找到host,但是会拒绝访问host。发现所有@Override函数中,get方法返回的都是null,prompt方法返回的都是false。
3. 为了判断这些Override的methods中,那些是有用的,在每个method中加入trace信息。发现只有promptYesNo(final String)会被调用到,输出如下图所示:
4. promptYesNo(final String)是向用户提出一个yes或者no的问题,来决定是否允许连接远程主机。这才是决定连接是否成功的一个关键函数。如果返回值为true,则允许连接;如果返回值为false,则拒绝连接。最后正确连接后的输出入下图所示:
Reference
JSch官网JSch API
相关文章推荐
- 【Jsch】使用SSH协议连接到远程Shell执行脚本
- java中如何使用jsch远程连接linux服务器并执行命令
- Java 使用SSH连接Linux 使用passwd命令修改密码
- java 使用jsch 远程链接linux执行命令
- java连接mysql使用 ssh
- ubuntu使用ssh连接远程电脑的方法
- JAVA实现远程SSH连接linux并执行命令
- linux 下使用SSH 连接远程主机 SCP 拷贝远程文件
- 使用 SSH 远程连接运行图形界面程序(ubuntu)
- java使用telnet连接远程计算机
- java 通过SSH方式连接远程主机并上传和下载文件
- 使用 SSH 远程连接运行图形界面程序(ubuntu)
- 如何使用SSH客户端putty远程连接linux
- 通过SecureCRT SSH远程连接服务器并使用Zmodem传送文件
- java 使用jsch 远程链接linux执行命令
- 使用JCONSOLE监控远程LINUX运行的JAVA进程,总是在报连接失败的错误。
- java中使用JSCH包,SFTP及SSH2文件操作及远程命令执行
- 简单介绍使用 ssh 连接远程服务器运行图形界面程序
- ubuntu使用ssh连接远程电脑的方法
- java 使用jsch 远程链接linux执行命令