package com.yangshenhui.linux;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class LinuxShellExecutor {
private Connection connection;
private String ip;
private int port;
private String username;
private String password;
private static final String CHARSET = "UTF-8";
private static final int TIME_OUT = 1000 * 60 * 3;
public LinuxShellExecutor(String ip, int port, String username,
String password) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
}
private boolean connection() throws IOException {
connection = new Connection(ip, port);
connection.connect();
return connection.authenticateWithPassword(username, password);
}
public LinuxShellReturnBean execute(String command)
throws LinuxShellException {
if (StringUtils.isEmpty(command)) {
return null;
}
InputStream stdOut = null;
InputStream stdErr = null;
LinuxShellReturnBean linuxShellReturnBean = new LinuxShellReturnBean();
int mark = -1;
try {
if (connection()) {
Session session = connection.openSession();
session.execCommand(command);
stdOut = new StreamGobbler(session.getStdout());
linuxShellReturnBean.setOutStr(processStream(stdOut));
stdErr = new StreamGobbler(session.getStderr());
linuxShellReturnBean.setOutErr(processStream(stdErr));
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
mark = session.getExitStatus();
}
} catch (Exception e) {
throw new LinuxShellException(e);
} finally {
if (connection != null) {
connection.close();
}
try {
if (stdOut != null) {
stdOut.close();
}
if (stdErr != null) {
stdErr.close();
}
} catch (Exception e) {
throw new LinuxShellException(e);
}
}
linuxShellReturnBean.setMark(mark);
return linuxShellReturnBean;
}
private String processStream(InputStream inputStream) throws Exception {
byte[] temp = new byte[1024 * 5];
StringBuilder stringBuilder = new StringBuilder();
while (inputStream.read(temp) != -1) {
stringBuilder.append(new String(temp, CHARSET));
}
return stringBuilder.toString().trim();
}
public class LinuxShellReturnBean {
private int mark;
private String outStr;
private String outErr;
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public String getOutStr() {
return outStr;
}
public void setOutStr(String outStr) {
this.outStr = outStr;
}
public String getOutErr() {
return outErr;
}
public void setOutErr(String outErr) {
this.outErr = outErr;
}
@Override
public String toString() {
return "ResultBean [mark=" + mark + ", outStr=" + outStr
+ ", outErr=" + outErr + "]";
}
}
public class LinuxShellException extends Exception {
private static final long serialVersionUID = 1L;
public LinuxShellException() {
}
public LinuxShellException(String message) {
super(message);
}
public LinuxShellException(String message, Throwable throwable) {
super(message, throwable);
}
public LinuxShellException(Throwable throwable) {
super(throwable);
}
}
public static void main(String args[]) throws LinuxShellException {
LinuxShellExecutor linuxShellExecutor = new LinuxShellExecutor(
"192.168.233.129", 22, "hadoop", "hadoop");
LinuxShellReturnBean linuxShellReturnBean = linuxShellExecutor
.execute("ls -l /home/hadoop/");
System.out.println(linuxShellReturnBean.getOutStr());
String[] s = StringUtils.split(linuxShellReturnBean.getOutStr(), "\n");
for (String string : s) {
System.out.print(string + "|");
}
}
}