can anybody show moe how to send from java ssh command ( example ssh [email protected] "ls" ) ? What class do I need ?

share|improve this question
Remote access for root? Generally not recommended. – Qwerky Jan 18 '11 at 12:30
you can use JSch library, but dont do directory listing with command "ls", better use sftp from the same library JSch, much easier to browse remote filesystem. – Ruslan Jan 18 '11 at 12:45

3 Answers

up vote 4 down vote accepted

You can use JSch or any other Java library. Google will help you.

Although, usually I find it more convenient to execute ssh commands from build script. E.g., there's an Ant task for that.

share|improve this answer
1  
+1 for the sshexec ant-task reference as a more convenient way – dimitrisli Jan 18 '11 at 11:50

Using sshj:

SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("nameOfServer");
ssh.authPublickey("userId");
Session session = ssh.startSession();
Command cmd = session.exec("yourCommand");
System.out.println(cmd.getOutputAsString());
session.close();
ssh.disconnect();
share|improve this answer

an other lib we use is http://www.ganymed.ethz.ch/ssh2/

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.