Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active January 3, 2016 11:29
Show Gist options
  • Save neilellis/8456087 to your computer and use it in GitHub Desktop.
Save neilellis/8456087 to your computer and use it in GitHub Desktop.
import net.schmizz.sshj.SSHClient
import java.io.File
fun File.scp(host: String = "localhost", user: String = "root", path: String = "~/", retry: Int = 3) {
val ssh: SSHClient = SSHClient();
ssh.addHostKeyVerifier { a, b, c -> true };
for ( i in 1..retry) {
try {
ssh.connect(host);
ssh use {
ssh.authPublickey(user);
ssh.newSCPFileTransfer()?.upload(this.getAbsolutePath(), path)
}
} catch (e: Exception) {
println("Retrying for the $i time to scp to $user@$host/$path - ${e.getMessage()}")
Thread.sleep(100)
}
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment