Issue
I am using grep4j to search log files in remote servers and show the results to users when they provide server details and hit on search from the browser. In my servlet , I am using the code below to service each search request like below,
import static org.grep4j.core.Grep4j.constantExpression;
import static org.grep4j.core.Grep4j.grep;
import static org.grep4j.core.fluent.Dictionary.on;
import static org.grep4j.core.fluent.Dictionary.options;
import static org.grep4j.core.fluent.Dictionary.with;
import org.grep4j.core.model.Profile;
import org.grep4j.core.model.ProfileBuilder;
import org.grep4j.core.result.GrepResults;
public class GrepSearcher {
private Profile remoteProfile = null;
GrepSearcher(){
}
//each request will call this method to initialize an SSH connection
GrepSearcher(String username, String password,String servername, String searchString){
this.remoteProfile = ProfileBuilder.newBuilder()
.name("DEV LOGS")
.filePath("LogFilepath")
.onRemotehost(servername)
.credentials(username,password)
.build();
}
public GrepResults getSearchResults(){
return grep(constantExpression("search query"),on(remoteProfile),with(options()));
}
}
is the SSH connection made to the server closed immediately after servicing the search request?. If so, should I somehow maintain the the profiles or is it done implicitely?. opening a new SSH connection for searching different strings on the same server seems that I am doing something wrong.
There will be only a handful of servers that all users will be searching for. So how can I make this search less overloading to the remote servers? May be keep a static Profile list and kill it after some idle time?
Thanks in Advance.
Solution
I have made a quick (well not that quick) peek into grep4j an i must say there is no way to execute multiple grep
commands on single connection. Internals of grep4j simply accept only single command and perfomrs it from begining to the end, from connectin establishement to connection closing.
The only thing it can do is to perform the same grep command on multiple profiles. So unless you wont modify the source code, you won be able to do what you want. Sorry
The best way to do this to search for different statefull SSH client (not only grep) for java, and execute any command from there. Google says there is plenty of such libs.
Answered By - Antoniossss Answer Checked By - Robin (WPSolving Admin)