scala-ssh icon indicating copy to clipboard operation
scala-ssh copied to clipboard

if there is a turtorial about how to login by ssh keyfile or codeconfig

Open yasz opened this issue 7 years ago • 1 comments

yasz avatar Oct 30 '18 03:10 yasz

i have an initial imperative way of doing this, so please provide a functional style. i will eventually, no time for now.

my method is simple,

  1. i keep a template of the hostConfig file which i fill out and write using twig.
  2. i always write a config file before calling SSH()

#keyfile should know: scala-ssh looks for files under ~/.scala-ssh

i use a template and it looks like this:

@(sshHost: String)
login-type  = keyfile
username    = root
keyfile     = ~/.ssh/id_rsa
host-name   = @sshHost
port        = 22
command-timeout = 5000
fingerprint = any

write the hostconfig file under ~/.scala-ssh keyfile is your ~/.ssh/id_rsa

example code

object SecureShellWrapper {
import java.io._
def writeHostFile(hostString) { 
   val file = new File(s"~/.scala-ssh/$hostString")
   val fw =   new BufferedWriter(new FileWriter(file))
   fw.write(twigPathToTemplate(hostString).render)
   fw.close()
 } 
def psa(host: String) { 
     writeHostFile(host)
     SSH(host) { client => 
      client.exec("ps -a").map { out => println(out.exitCode) } //
   }
//..
}

Openpalm avatar Sep 30 '19 10:09 Openpalm