Results 1 to 5 of 5
  1. #1
    idan hen is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Talking j2ssh to run multiple commands

    Hi ,

    i am new here , so if i have mistake , i am sorry :)
    i wrote a little program to run commands on a remote unit , i used the j2ssh .
    it all works well for 1 commmand , and the output is just as expected.
    when running the program with more than 2 commands , i dont get the output String .

    working Command :
    cd /usr/cti/bin;ls -la;

    no 'ls -la' output :
    cd /usr/cti/bin;pwd;ls -la;


    Java Code:
           if ( CreateSSHconnection(_UnitIP) )
           {
               try {
                    SessionChannelClient session =  ssh.openSessionChannel();
                    InputStream in = session.getInputStream(); // start input stream for ssh result
                    BufferedReader br = new BufferedReader(new InputStreamReader(in)); // read to buffer from the stream
                    StringBuffer buffer = new StringBuffer();
                    logger.debug("Starting to execute commands on Unit : " + _UnitIP);
                    session.executeCommand(_command);  // execute the command via SSH session
                    while (((line = br.readLine()) !=  null)) // read from the buffer of the stream the line
                                        buffer.append(line + " \n "); // append the result line to the String buffer.
                    output = buffer.toString();	 // Convert the output line from stringbuffer to String.
                    session.close();  // close the SSH session.
                    ssh.disconnect();
                    return output;
               }
               catch(Exception exGeneral){
                   logger.error("failed to execute the command : " + exGeneral.getMessage());
                   return null;
               }
           }
           else{
               logger.error("SSH connection was not created ! ");
               return null;
           }
    any help will be great :) , thanks !

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You would have to read the API specs for the classes you are using (the jars you downloaded) to find out if that method supports multiple commands and if so, how to pass those commands and get output.

  3. #3
    idan hen is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    i wish it had more Docs , but i reviewed it , and searched the net.
    i thought maybe someone already had this issue , or something like that .

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I didn't check the docs for the package you downloaded but a rule of thumb is never use jars that are not well documented.
    The whole thing becomes a guessing game. How would you know which methods do what?
    If that project doesn't have proper documentation then it's best to just find another.

  5. #5
    idan hen is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    well i couldn't find a similiar package that i can connect and run multiple commands in 1 session .
    in all i have to connect and run 1 command , and thats not what i want .
    i want to run multiple command .

    and i am so close to get it , only the output make me trouble :)

Similar Threads

  1. InFile Commands not being recognized.
    By Tenn in forum New To Java
    Replies: 4
    Last Post: 02-13-2009, 03:50 PM
  2. Execute the commands in Jsp Program
    By swetha_2008 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2008, 06:04 AM
  3. Windows Runtime Commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:33 AM
  4. J2ssh 0.2.9
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-23-2007, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •