**
Java中采用Ganymed技术实现ssh远程访问
**
连接类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/**
* <p>Description:Ganymed技术实现的ssh远程访问</p>
* <p>Copyright:Copyright (c) 2018</p>
* <p>Company:xazhe </p>
* @author xazhe
* @createtime 2018年06月05日
*/
public class GanymedSshUtil {
/**
* @Title:初始化连接
* @Copyright: Copyright (c) 201806
* @Company: xazhe
* @author xazhe
* @version 1.0
* @param ip
* IP地址
* @param username
* 主机用户
* @param password
* 用户密码
* @param port
* 端口号(默认22)
* */
public static ch.ethz.ssh2.Connection getConnection(String ip,
String username, String password,Integer port) throws Exception {
try {
if(port==null || port==0){
port = 22;
}
Connection mConn = new Connection(ip, port);
mConn.connect();
mConn.authenticateWithPassword(username, password);
return mConn;
} catch (Exception e) {
throw new Exception(e);
}
// return _connPool.getConnection(ip, username, password);
}
/**
* @Title:关闭连接
* @Copyright: Copyright (c) 201806
* @Company: xazhe
* @author xazhe
* @version 1.0
* @param connection
* 连接
*/
public static void closeConnection(Connection conn) {
if (conn != null) {
conn.close();
}
}
/**
* @Title:获得Shell命令返回值
* @Copyright: Copyright (c) 201806
* @Company: xazhe
* @author xazhe
* @version 1.0
* @param Connection
* 连接
* @param shellCmd
* shell命令
*/
public static String executeShellCmd(Connection conn, String shellCmd) {
String ret = "";
if (shellCmd == null || shellCmd.trim().equals("")) {
return ret;
}
try {
Session sess = conn.openSession();
sess.execCommand(shellCmd);
// 错误命令输出流
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader brErr = new BufferedReader(new InputStreamReader(
stderr));
String line = null;
while ((line = brErr.readLine()) != null) {
if (!ret.equals("")) {
ret = ret + "\n";
}
ret = ret + line;
}
// 正确命令输出流
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader brOut = new BufferedReader(new InputStreamReader(
stdout));
while ((line = brOut.readLine()) != null) {
if (!ret.equals("")) {
ret = ret + "\n";
}
ret = ret + line;
}
sess.close();
if (stdout != null) {
stdout.close();
}
if (brOut != null) {
brOut.close();
}
} catch (IOException e) {
e.printStackTrace(System.err);
throw new RuntimeException(e);
}
return ret;
}
}
测试类
public class Test {
public static void main(String[] args) {
String hostIp = "10.163.10.1";
String hostUser = "xiaoazhe";
String password = "xiaoazhe";
String cmd = "ls /";
try{
connection = GanymedSshUtil.getConnection(hostIp, hostUser, passWord);
String result = GanymedSshUtil.executeShellCmd(connection, cmd);
System.out.println("result:" + result);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection != null){
GanymedSshUtil.closeConnection(connection);
}
}
}
}
在有该主机远程访问权限的情况下,通过ip,用户名,密码即可连接到主机,执行命令并且获取返回值
需要注意的是Ganymed默认一次连接只能执行一条命令,如果要执行多条命令,中间可以加入
线程休眠
Thread.sleep(1000);