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;
}
}