基本上,我连接到远程服务器并从日志文件夹中获取所有日志文件名。我在列表中添加了这些文件名,现在我想在该文件上读取或尾部-f,我应该寻找是否有任何文件遇到"错误"然后我应该能够写出其他文件了。 我想使用多线程,我想创建线程来单独读取每个日志文件。
例如:
public List<String> excuteScript(String fileName) {
List<String> result = new ArrayList<String>();
try {
JSch jSch = new JSch();
Session session = jSch.getSession(USERNAME, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
session.connect();
//create the excution channel over the session
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
// Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
InputStream in = channelExec.getInputStream();
// Set the command that you want to execute
// In our case its the remote shell script
channelExec.setCommand("sh " + fileName);
// Execute the command
channelExec.connect();
System.out.println("connected");
// Read the output from the input stream we set above
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
//Read each line from the buffered reader and add it to result list
// You can also simple print the result here
while ((line = reader.readLine()) != null) {
if(line.contains("Error") || line.contains("Exception")) {
System.out.println(line);
result.add(line);
}
}
//retrieve the exit status of the remote command corresponding to this channel
int exitStatus = channelExec.getExitStatus();
//Safely disconnect channel and disconnect session. If not done then it may cause resource leak
channelExec.disconnect();
session.disconnect();
if(exitStatus < 0) {
// System.out.println("Done, but exit status not set!");
}
else if(exitStatus > 0) {
// System.out.println("Done, but with error!");
}
else {
// System.out.println("Done!");
}
}
catch(Exception e) {
System.err.println("Error: " + e);
}
return result;
}