package lwc;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by lwc on 16-2-19.
*/
public class TailThread implements Runnable {
/**
* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)
*
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
String host;
String user;
String psw;
int port;
String command;
String interestStr;
TailThread(String host, String user, String psw, int port, String command, String interestStr) {
this.host = host;
this.user = user;
this.psw = psw;
this.port = port;
this.command = command;
this.interestStr = interestStr;
}
public void run() {
this.exec(host, user, psw, port, command, interestStr);
}
/**
* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)
*
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public String exec(String host, String user, String psw, int port, String command, String interestingStr) {
byte[] tmp = new byte[1024]; //读数据缓存
StringBuffer strBuffer = new StringBuffer(); //执行SSH返回的结果
Session session = null;
ChannelExec openChannel = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(psw);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
ChannelExec ssh = openChannel;
//返回的结果可能是标准信息,也可能是错误信息,所以两种输出都要获取
//一般情况下只会有一种输出.
//但并不是说错误信息就是执行命令出错的信息,如获得远程java JDK版本就以
//ErrStream来获得.
InputStream InputStream = ssh.getInputStream();
InputStream ErrStream = ssh.getErrStream();
ssh.setCommand(command);
ssh.connect();
//开始获得SSH命令的结果
while (true) {
//获得错误输出
while (ErrStream.available() > 0) {
int i = ErrStream.read(tmp, 0, 1024);
if (i < 0) {
break;
}
strBuffer.append(new String(tmp, 0, i));
// System.out.println("Error" + strBuffer.toString());
if(strBuffer.toString().contains(interestingStr)) {
LogListener.flag = true;
System.out.println(this.toString() + "&&&" + strBuffer.toString());
Thread.interrupted();
}
strBuffer = new StringBuffer();
}
//获得标准输出
while (InputStream.available() > 0) {
int i = InputStream.read(tmp, 0, 1024);
if (i < 0) {
break;
}
strBuffer.append(new String(tmp, 0, i));
// System.out.println("Correct;" + strBuffer.toString());
if(strBuffer.toString().contains(interestingStr)) {
LogListener.flag = true;
System.out.println(this.toString() + "&&&" + strBuffer.toString());
Thread.interrupted();
}
strBuffer = new StringBuffer();
}
if (ssh.isClosed()) {
// System.out.println("exit-status: " + ssh.getExitStatus());
break;
}
try {
Thread.sleep(100);
} catch (Exception ee) {
}
}
return strBuffer.toString();
} catch (JSchException | IOException e) {
System.out.println(e.getMessage());
} finally {
if (openChannel != null && !openChannel.isClosed()) {
openChannel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
return strBuffer.toString();
}
@Override
public String toString() {
return "TailThread{" +
"host='" + host + '\'' +
", interestStr='" + interestStr + '\'' +
'}';
}
}
<pre name="code" class="java">package lwc;
/**
* Created by lwc on 16-2-19.
*/
public class LogListener {
public static boolean flag = false;
public int hostNum = 4; //default
public String[] hosts = new String[hostNum];
public int[] port = new int[hostNum];
public String path[] = new String[hostNum];
public String[] usr = new String[hostNum];
public String[] pwd = new String[hostNum];
public String interestingString = "Exception";
public LogListener() {
defaultInit();
}
public LogListener(int hostNum, String[] hosts, int[] port, String[] path, String[] usr, String[] pwd, String interestingString) {
this.hostNum = hostNum;
this.hosts = hosts;
this.port = port;
this.path = path;
this.usr = usr;
this.pwd = pwd;
this.interestingString = interestingString;
}
public int getHostNum() {
return hostNum;
}
public String[] getHosts() {
return hosts;
}
public int[] getPort() {
return port;
}
public String[] getPath() {
return path;
}
public String[] getUsr() {
return usr;
}
public String[] getPwd() {
return pwd;
}
public String getInterestingString() {
return interestingString;
}
public void setHostNum(int hostNum) {
this.hostNum = hostNum;
}
public void setHosts(String[] hosts) {
this.hosts = hosts;
}
public void setPort(int[] port) {
this.port = port;
}
public void setPath(String[] path) {
this.path = path;
}
public void setUsr(String[] usr) {
this.usr = usr;
}
public void setPwd(String[] pwd) {
this.pwd = pwd;
}
public void setInterestingString(String interestingString) {
this.interestingString = interestingString;
}
public void defaultInit() {
hostNum = 4;
for (int i = 0; i < hostNum; i++) {
hosts[i] = "172.168.256." + i);
port[i] = 22;
path[i] = "/var/log/hadoop/namenode-server-yarn.log";
usr[i] = "root";
pwd[i] = "password";
}
interestingString = "Exception";
}
Thread[] threads = new Thread[hostNum];
public void start() {
for (int i = 0; i < hostNum; i++) {
threads[i] = new Thread(new TailThread(hosts[i], usr[i], pwd[i], port[i], "tail -f " + path[i], interestingString));
threads[i].start();
}
// while (true) {
System.out.println(flag);
// if (flag) {
// for(int i = 0; i < hostNum; i++) {
// if(threads[i].isAlive()) {
// threads[i].interrupt();
// threads[i].stop();
// }
// }
// break;
// } else {
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
}
public boolean pause() {
for (int i = 0; i < hostNum; i++) {
if (threads[i].isAlive()) {
threads[i].interrupt();
threads[i].stop();
}
}
return flag;
}
}
package lwc;
/**
* Created by lwc on 16-2-18.
*/
public class SSHHelper {
public static void main(String args[]) {
LogListener logListener = new LogListener();
logListener.setInterestingString("OK3r5454tt");
logListener.start();
System.out.println("123456");
//run sql
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(logListener.pause());
}
}