can anybody show moe how to send from java ssh command ( example ssh [email protected] "ls" ) ? What class do I need ?
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With