[B]
Hi experts,
I have prepared a Java Class with name SshAgent. In this same class we are passing arguments hostname,username and password and connecting via SSH. The issue is getting while executing command to the server.
Here is the code and the error is getting on highlighted portion.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jscape.inet.ssh.SshException;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SshAgent {
private String hostname;
private String username;
private String password;
private Connection connection;
public SshAgent(String hostname,String username,String password){
this.hostname= hostname;
this.username= username;
this.password= password;
}
public boolean connect() throws SshException {
{
try{
connection = new Connection(hostname);
connection.connect();
//Authenticate
boolean result =connection.authenticateWithPassword(username, password);
System.out.println("Connection result :" + result);
return result;
} catch(Exception e){
throw new SshException( "An exception occurred while trying to connect to the host: " + hostname + ", Exception=" + e.getMessage(), e );
}
}
}
public String executeCommand(String command) throws SshException{
try {
// Open Session
Session session = connection.openSession();
// execute Command
session.execCommand(command);
// read the result
StringBuilder sb = new StringBuilder();
InputStream stdout = new StreamGobbler( session.getStdout() );
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
String line = br.readLine();
while( line != null )
{
sb.ensureCapacity(1000);
sb.append(line);
} // Debug: dump the exit code
System.out.println("Exit Code :" + session.getExitStatus());
// Close the session
session.close();
// Return the results to the caller
return sb.toString();
} catch (IOException e) {
throw new SshException( "An exception occurred while executing the following command:"+ command + ". Exception = " + e.getMessage(), e );
}
}
public void logout() throws SshException{
{ try{
connection.close();
}catch (Exception e){
throw new SshException( "An exception occurred while closing the SSH connection: " + e.getMessage(), e );
}
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class WorkingWithSshAgent {
public static void main(String[] args) {
{ try{
String tmpHost = null;
String tmpUser = null;
String tmpPass = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Hostname: ");
tmpHost = reader.readLine();
System.out.print("Username: ");
tmpUser = reader.readLine();
System.out.print("Password: ");
tmpPass = reader.readLine();
SshAgent sshAgent = new SshAgent( tmpHost, tmpUser, tmpPass );
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println("free memory: " + freeMemory / 1024);
System.out.println("allocated memory: " + allocatedMemory / 1024);
System.out.println("max memory: " + maxMemory /1024);
System.out.println("total free memory: " +
(freeMemory + (maxMemory - allocatedMemory)) / 1024);
if( sshAgent.connect() )
{
String processInfo = sshAgent.executeCommand( "ls -l" );
System.out.println( "Process Info: " + processInfo );
// Logout
sshAgent.logout();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
The error is getting generated in above code in below portion
while( line != null )
{
sb.ensureCapacity(1000);
sb.append(line);
}
The error We are getting are.........
Hostname: 147.128.162.34
Username: username
Password: **********
free memory: 15486
allocated memory: 15872
max memory: 253440
total free memory: 253054
Connection result :true
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unk nown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at SshAgent.executeCommand(SshAgent.java:60)
at WorkingWithSshAgent.main(WorkingWithSshAgent.java: 37)
Pls help in identifying what wrong in my code here.