java读取远程服务器上的log日志
上一篇 /
下一篇 2012-07-16 11:49:41
/ 个人分类:java
通过ssh链接到对应的服务器上,然后通过tail -f xx.log 来动态读取服务器上的log日志
public class Basic{
private static Connection conn = null;
private String hostname = "";
private String username = "";
private String password = "";
private String[] commands;
/**
*
* @param hostname
* @return
*/
private Connection getConnection(String hostname) {
if (conn == null) {
conn = new Connection(hostname);
}
return conn;
}
public void exec(String[] commands) {
for (int i = 0; commands != null && i < commands.length; i++) {
Connection conn = getConnection(hostname);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(
username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session sess = conn.openSession();
sess.execCommand(commands[i]);
I