This code helps us
to search for the particular key in the log files by connecting to
that servers and getting the entries that matches our search key.
This can connect to different servers and fetch the results based on
our confiuguration.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.Writer; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; /* * *Dependency : This java code is dependent on the below jar file which can be downloaded *Jar Name : ganymed-ssh2-build210.jar * */ public class FilePreviewer { private File outputFile; private Writer outputStream; public static String newline = System.getProperty("line.separator"); public FilePreviewer() { } public FilePreviewer(String user, String pass) { String ipAd[]={"IP1","IP2"}; int x=1, numOfServers=x+1; String keyText="searchKey"; //search key while(x<numOfServers) //incase if we want to search in more than one server in a loop { try { Connection connection = new Connection(ipAd[x]); connection.setTCPNoDelay(true); connection.connect(); Authentication a=new Authentication(pass); System.out.println("Connection Established: "+ipAd[x]); outputFile=new File("output_"+x+".txt");//Output File name boolean flag=false; if(connection.isAuthMethodAvailable(user, "password")) flag = connection.authenticateWithPassword(user,pass); if(!flag) { flag = connection.authenticateWithKeyboardInteractive(user, a); } if (flag == true) { Session session = connection.openSession(); String s3 = "grep -i '"+keyText+"' /home/userId/folder/filename.log"; //Configure the search command to point to any file. session.execCommand(s3); StreamGobbler streamgobbler = new StreamGobbler(session.getStdout()); BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(streamgobbler)); outputStream = new BufferedWriter(new FileWriter(outputFile)); String cmdOut=""; int count=0; do { cmdOut = bufferedreader.readLine(); if(cmdOut==null) break; System.out.println(cmdOut); outputStream.write(cmdOut+newline); count++; } while (true); outputStream.close(); session.close(); connection.close(); if (count==0) { System.out.println("KeyText not found"); } else { System.out.println("Found: "+count); count=0; } } else { System.out.println(" Authentication Failed"); } } catch(Exception e) { System.out.println("n"+e.toString()); } x=x+6; } } public static void main(String[] args) { new FilePreviewer("user","pwd"); //user name and password } } //Code that takes care of KeyBoard Interactive Authentication class Authentication implements ch.ethz.ssh2.InteractiveCallback { int promptCount; String pwd; public Authentication(String password) { promptCount = 0; pwd = password; } public String[] replyToChallenge(String name, String instruction, int numPrompts, String prompt[], boolean echo[]) throws Exception { String result[] = new String[numPrompts]; for(int i = 0; i < numPrompts; i++) { if(prompt[i].contains("Password:")) { result[i] = pwd; promptCount++; } } return result; } }