I am currently trying to access a Linux server and run some commands. The general idea is to search for a log file containing certain text and then access that log, search for the requisite text and store all the data currently displayed on the screen in a String.
This approach works fine through PuTTY but now the requirement is different.
I have accomplished this task on other commands like ls
and other commands that provide a direct output. However, I am having no luck with commands like less
or more
.
I have of course tried on sending the entire contents of the log to an array list and then searching for my text (seeing as I will have a relative idea of the position of the data once the 1st line is found). However, this takes a huge amount of time as the log size varies from 10 Mb to 750 Mb.
I would appreciate if anyone can think of another approach or provide any ideas towards a feasible way to this approach.
For reference: Code I have tried
public class check {
private Session session;
private String username = "user";
private String password = "pass";
private String hostname = "host";
public SSHConnectionManager()
{ }
public SSHConnectionManager(String hostname, String username, String password)
{
this.hostname = hostname;
this.username = username;
this.password = password;
}
public void open() throws JSchException
{
open(this.hostname, this.username, this.password);
}
public void open(String hostname, String username, String password) throws JSchException
{
JSch jSch = new JSch();
session = jSch.getSession(username, hostname, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no"); // not recommended
session.setConfig(config);
session.setPassword(password);
System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
session.connect();
System.out.println("Connected!");
}
public String runCommand(String command) throws JSchException, IOException
{
String ret = "";
if (!session.isConnected())
throw new RuntimeException("Not connected to an open session. Call open() first!");
ChannelExec channel = null;
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
PrintStream out = new PrintStream(channel.getOutputStream());
InputStream in = channel.getInputStream(); // channel.getInputStream();
channel.connect();
ret = getChannelOutput(channel, in);
channel.disconnect();
System.out.println("Finished sending commands!");
return ret;
}
private String getChannelOutput(Channel channel, InputStream in) throws IOException
{
byte[] buffer = new byte[1024];
StringBuilder strBuilder = new StringBuilder();
String line = "";
while (true){
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) {
break;
}
strBuilder.append(new String(buffer, 0, i));
System.out.println(line);
}
if(line.contains("logout")){
break;
}
if (channel.isClosed()){
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee){}
}
return strBuilder.toString();
}
public void close()
{
session.disconnect();
System.out.println("Disconnected channel and session");
}
public static void main(String[] args)
{
SSHConnectionManager ssh = new SSHConnectionManager();
try {
ssh.open();
String ret = ssh.runCommand("ls -l");
System.out.println(ret);
ret=ssh.runCommand("cd *move to path of log*");
System.out.println(ret);
ret=ssh.runCommand("less *log name*");
System.out.println(ret);
ret=ssh.runCommand("/*searchtext*");
System.out.println(ret);
ssh.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I get the necesaary output for my ls command but not for any of the others.