5

I'm currently using a library for SSH within Java but it seems to be lacking the ability to do multipart commands (such as if I do passwd user I have no way of then entering the password twice to change it to because it makes you start a new session each time you enter a command). I really need this functionality for the current app I'm working on and there seems to be a lot of SSH libraries for Java but I am not sure which ones would allow this as some don't seem to.

Any advice on a good library to look at for this would be greatly appreciated.

To the close patrol: if you are going to try to close this, at least cite a thread that asks the same question as mine, not just a general "whats a good Java ssh library" question

Rick
  • 16,612
  • 34
  • 110
  • 163
  • possible duplicate of [SSH library for Java](http://stackoverflow.com/questions/995944/ssh-library-for-java) – Brian Roach Jun 01 '11 at 01:36
  • 3
    thanks for trying to close my thread by citing one that is not the same, but please read the thread you cited and you will see that it is not specifying the same parameters I am, I was aware of that thread and it did not ask my specific question – Rick Jun 01 '11 at 02:14

3 Answers3

2

I am using the Ganymede SSH-2 library with great success. However the password prompt shouldn't appear to the application at all, it should be part of the connection setup negotiation.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • thanks I will look at Ganymede (Ganymed ?), but I think you misread what I said, it is an example command where I am trying to change a user's password using `passwd user`, not enter a password to connect – Rick Jun 01 '11 at 00:59
  • 1
    for ref: http://www.cleondris.ch/opensource/ssh2/ since I had a little trouble finding the main page for the project – Rick Jun 01 '11 at 01:04
  • @Rick OK I see. Good luck with it. – user207421 Jun 01 '11 at 01:07
0

you can run interactive commands like "passwd user " by using

session.getStdin().write() refer following snipped 
connection = new Connection( host,port );
            connection.connect();
            // Authenticate
   boolean result = connection.authenticateWithPassword( username, password );
 Session session = connection.openSession();
session.requestPTY("bash");
session.execCommand( "passwd testuser" );
session.getStdin().write("newPassword\n".getBytes()); // //for new password
session.getStdin().write("newPassword\n".getBytes()); //for retype new password

 System.out.println( "ExitCode: " + session.getExitStatus() );
session.close();
connection.close();
Prasad Parab
  • 437
  • 1
  • 7
  • 26
0

After a lot of searching I found this: http://code.google.com/p/enchanter/ which looks like its exactly what I need so I figured I'd post this here for reference

Rick
  • 16,612
  • 34
  • 110
  • 163