Java中采用Ganymed技术实现ssh远程访问

**

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值