Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send ssh command from Java code [closed]

Tags:

java

ssh

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

like image 289
Damir Avatar asked May 19 '26 23:05

Damir


2 Answers

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();
like image 138
snakile Avatar answered May 21 '26 12:05

snakile


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.

like image 45
Nikita Rybak Avatar answered May 21 '26 13:05

Nikita Rybak