Scala SSH: Connecting to hosts with public key exchange

1.5k Views Asked by At

I am a noob to scala and trying to figure out how to use Scala SSH to connect to a server with SSH client keys / public key exchange configured. My goal is to create a successful SSH connection to a server with proper config of client keys without needing to provide a password in the application. In the case of servers that do no have a proper client key configuration, the expectation would be that the user has set up a proper Host Config File. This is what I have so far:

try {
   SSH("server_with_client_keys_configured") { client =>
     val x = client.exec("ls -a")
     println(x.left)
     x.right.map { result =>
       println("Result:\n" + result.stdOutAsString())
   }
 } catch {
   case e: Exception => println(e)
 }

Now if I use that snippet to connect to a host when I have a proper host config file it works. But, there are a few things I can't figure out:

  1. in the case of a host with client keys configured, and no host file, the code block does not execute
  2. errors come in the left projection (strange to me, why not an exception? seems easy to miss the error)
    • if I use a host config file for a host with client keys configured, but provide the login-type: keyfile field and omit the password field then I get:

LeftProjection(Left(Could not authenticate (with keyfile) to server_with_client_keys_configured:22 due to net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods))

Finally, if this is just a case of the Scala SSH not having support for public key exchange, then can someone suggest an alternative?

0

There are 0 best solutions below