Created
February 23, 2019 04:16
-
-
Save lynas/a4b9b2b875c0ade13f1f98a3ec443aec to your computer and use it in GitHub Desktop.
Read log file from remote machine real time with jsch extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lynas; | |
import com.pastdev.jsch.DefaultSessionFactory; | |
import com.pastdev.jsch.command.CommandRunner; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
import static java.nio.file.StandardOpenOption.APPEND; | |
import static java.nio.file.StandardOpenOption.CREATE; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Main2 { | |
public static void main(String[] args) throws Exception { | |
DefaultSessionFactory sessionFactory = new DefaultSessionFactory( | |
"username", "hostIp", 22 | |
); | |
Map props = new HashMap<String, String>(); | |
props.put("StrictHostKeyChecking", "no"); | |
sessionFactory.setConfig(props); | |
sessionFactory.setPassword("password"); | |
long startLine = 1; | |
long endLine = 1; | |
while (true) { | |
CommandRunner runner = new CommandRunner(sessionFactory); | |
String command = "sed -n '"+startLine+",$p;"+endLine+"q' /remote/dir/logFileName.log"; | |
CommandRunner.ExecuteResult result = runner.execute(command); | |
if (result.getStderr().isEmpty()) { | |
if (result.getStdout().isEmpty()) { | |
continue; | |
}else { | |
startLine += 1; | |
endLine += 1; | |
} | |
Files.write(Paths.get("e:\\local\\directory\\server.log"), result.getStdout().getBytes(), APPEND); | |
} else { | |
System.out.println(result.getStderr()); | |
} | |
runner.close(); | |
Thread.sleep(100); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment