import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.SFTPClient
import net.schmizz.sshj.userauth.keyprovider.KeyProvider
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile
import net.schmizz.sshj.userauth.password.PasswordFinder
final SSHClient ssh = new SSHClient()
ssh.loadKnownHosts(new File("path/to/known_hosts"))
ssh.connect("server")
try {
OpenSSHKeyFile keyProvider = new OpenSSHKeyFile()
keyProvider.init(
new File("path/to/key/file"),
new StaticPasswordFinder("secretpassphrase")
)
ssh.authPublickey("username",keyProvider)
final SFTPClient sftp = ssh.newSFTPClient()
try {
// do stuff
}
} finally {
sftp.close()
}
} finally {
ssh.disconnect()
}
class StaticPasswordFinder implements PasswordFinder {
private char[] password;
public StaticPasswordFinder( String password ) {
this.password = password.toCharArray()
}
public char[] reqPassword(net.schmizz.sshj.userauth.password.Resource<?> resource) {
return password;
}
public boolean shouldRetry(net.schmizz.sshj.userauth.password.Resource<?> resource) {
return false;
}
}