LinuxShellExecutor

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 + "|");
		}
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值