Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to connect and fire command to SSH server using Gyanmed SSH2 API.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to connect and fire command to SSH server using Gyanmed SSH2 API.

    [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.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How to connect and fire command to SSH server using Gyanmed SSH2 API.

    Please wrap code in [code=Java] [/code] tags to preserve formatting.

    Just looking at the error and scanning briefly over the code, an OutOfMemoryError suggests that you are trying to load way too much into memory, or at minimum preparing too load to much. Why do you ensure capacity every loop? It may or may not do anything, and isn't likely to help, removing that may solve that problem altogether.

    Also try to create a SSCCE (Simple Self-Contained Complete Example) that demonstrates just reading the file if the problem persists.
    Last edited by Tjstretch; July 4th, 2012 at 08:57 AM. Reason: Typo

Similar Threads

  1. Replies: 7
    Last Post: December 28th, 2012, 10:48 AM
  2. Java Code to connect remote server
    By idealguy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 3rd, 2012, 05:59 AM
  3. Connect to SQL server 2008
    By vahidbakhtiary in forum JDBC & Databases
    Replies: 1
    Last Post: July 2nd, 2012, 11:07 AM
  4. Connect Four GUI
    By gromacs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 10:42 AM
  5. Cannot Connect to Server
    By Brt93yoda in forum Java Networking
    Replies: 0
    Last Post: December 9th, 2010, 05:54 PM