2

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

4
  • possible dublicates stackoverflow.com/questions/570450/… and stackoverflow.com/questions/995944/ssh-library-for-java Commented Jan 18, 2011 at 11:33
  • Remote access for root? Generally not recommended. Commented Jan 18, 2011 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. Commented Jan 18, 2011 at 12:45
  • Does this answer your question? SSH library for Java Commented Jul 20, 2020 at 8:02

3 Answers 3

14

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the option. Just bear in mind that this library has 5 library dependencies.
6

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.

1 Comment

+1 for the sshexec ant-task reference as a more convenient way
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.