Es necesaria la dependencia
1 2 3 |
dependencies { implementation("com.hierynomus:sshj:0.27.0") } |
Código que permite conectarse a un servidor SSH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import net.schmizz.sshj.SSHClient import net.schmizz.sshj.connection.channel.direct.Session import java.io.IOException import java.io.InputStream fun main() { val host = "tu.servidor.ssh.com" val user = "tu_usuario_ssh" val password = "tu_contraseña_ssh" val command = "ls -l" val ssh = SSHClient() ssh.addHostKeyVerifier { _, _, _ -> true } try { ssh.connect(host) ssh.authPassword(user, password) val session: Session = ssh.startSession() val cmd: Session.Command = session.exec(command) val inputStream: InputStream = cmd.inputStream val output = inputStream.bufferedReader().readText() cmd.join(5, java.util.concurrent.TimeUnit.SECONDS) println("Salida del comando:") println(output) } catch (e: IOException) { e.printStackTrace() } finally { ssh.disconnect() } } |