devops-java远程调用ssh2执行Linux命令

使用下面这个Java包
maven

      
        ch.ethz.ganymed
        ganymed-ssh2
        262
        compile
      

ssh 两种方式:1.用户名密码。2.做面密配置,

api使用方式

//name password
private Connection getConnectionWithPassword() {
        conn = new Connection(host, sshPort);
        try {
            conn.connect();
            boolean auth = conn.authenticateWithPassword(userName, password);
            if (auth) {
                logger.debug("{},主机认证成功", resourcesEntity.getHost());
                return conn;
            } else {
                throw new DefaultBusiException("ssh连接失败,请检查用户名、密码、ssh端口");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new DefaultBusiException("ssh连接失败,请检查用户名、密码、ssh端口");
        }
    }

//publickey
private Connection getConnectionWithPublicKey() {
        conn = new Connection(host, sshPort);
        try {
            conn.connect();
            boolean auth = conn.authenticateWithPublicKey(userName, new File(sshPrivateKeyPath), sshPassword());
            if (auth) {
                logger.debug("{},主机认证成功", resourcesEntity.getHost());
                return conn;
            } else {
                throw new DefaultBusiException("ssh连接失败,请检查用户名、密码、ssh端口");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new DefaultBusiException(e);
        }
    }

你可能感兴趣的:(devops-java远程调用ssh2执行Linux命令)