欢迎使用CSDN-markdown编辑器

1 篇文章 0 订阅
1 篇文章 0 订阅

package com.system.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mesosphere.marathon.client.utils.ModelUtils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class RemoteShellExecutor {

//private static Class MesosaAency;

private Connection conn;

/** 远程机器IP */
private String ipAddr; 

private String charset = Charset.defaultCharset().toString();  

/** 用户名 */
private String userName; 

/** 密码 */
private String password;  

/**
 * 构造函数
 * @param ipAddr
 * @param userName
 * @param password
 * @param charset
 */
public RemoteShellExecutor(String ipAddr, String userName, String password,  
        String charset) {  
    this.ipAddr = ipAddr;  
    this.userName = userName;  
    this.password = password;  
    if (charset != null) {  
        this.charset = charset;  
    }  
}  

/**
 * 登录
 * @return
 * @throws IOException
 */
public boolean login() throws IOException {  
    conn = new Connection(ipAddr);  
    conn.connect(); // 连接  
    return conn.authenticateWithPassword(userName, password); // 认证  
}  

/**
 * 执行脚本  
 * @param cmds
 * @return
 * @throws Exception 
 */
public String executeSuccess(String cmds) throws Exception {  
    InputStream in = null;  
    String result = "";  
    try {  
        if (this.login()) {  
            Session session = conn.openSession(); // 打开一个会话  
            session.execCommand(cmds);  

            in = session.getStdout(); 
            result = this.processStream(in, this.charset); 
            in.close();
            conn.close();
            session.close();  
        }else {
             throw new Exception("登录远程机器失败" + ipAddr); // 自定义异常类 实现略
        }  
    } catch (IOException e1) {  
        e1.printStackTrace();  
    }
    return result;  
}  
/**
 * 执行脚本  
 * @param cmds
 * @return
 * @throws Exception 
 */
public void execute(String cmds) throws Exception {  
    try {  
        if (this.login()) {  
            Session session = conn.openSession(); // 打开一个会话  
            session.execCommand(cmds);  
        }else {
            throw new Exception("登录远程机器失败" + ipAddr); // 自定义异常类 实现略
        }  
    } catch (IOException e1) {  
        e1.printStackTrace();  
    }
}  

/**
 * 
 * @param in
 * @param charset
 * @return
 */
public String processStream(InputStream in, String charset) {
    InputStream    stdout = new StreamGobbler(in);  
    StringBuffer buffer = new StringBuffer();;  
    try {  
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));  
        String line=null;  
        while((line=br.readLine()) != null){  
            buffer.append(line+"\n");  
        }  
    } catch (UnsupportedEncodingException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return buffer.toString();  
}  

public static void main(String args[]) throws Exception {
    RemoteShellExecutor executor = new RemoteShellExecutor("192.168.163.130", "root", "root","utf-8");

    //String ports = executor.executeSuccess("bash gottyPort.sh mesos-fceadadf-b529-4db0-9046-1a02012ebbef-S0.07013d60-26ca-4645-90ad-ef058f9240de");

}

public String getIpAddr() {
    return ipAddr;
}

public void setIpAddr(String ipAddr) {
    this.ipAddr = ipAddr;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值