SSH command from within a Scala program with pure Scala API or Ammonite API

2.2k views Asked by At

I would like to perform an SSH command and then execute some command on a distant machine from within scala. Both the scala API and Scala Ammonite provide way to execute system command. However I am having problem with ssh. How do i run an ssh command and then run command and get the result back in from within my scala program ?

EDIT1

found the following in that post Problems using scala to remotely issue commands via ssh

"ssh [email protected] 'mkdir Desktop/test'".!

I wonder how can this be done with scala Amonite API. I have not been able to find the way.

2

There are 2 answers

0
MaatDeamon On BEST ANSWER

So far found what i wanted with the Scala API: http://www.scala-lang.org/api/2.12.1/scala/sys/process/ProcessBuilder.html e.g.

 import scala.sys.process.ProcessBuilder  
 import scala.sys.process._ 
 import scala.sys.process

 "ssh 192.168.0.103  ls IdeaProjects" lineStream 

It works as a charm Executed from one of my machine to the other it worked flawlessly. This require a phraseless ssh to be set up in advance. I am the same use on both machine to other i would have had to specify the user as well

If someone can complement the Answer and explain how to do it with the Ammonite API that would be great.

0
Michaelzh On

You can use Cable to do your SSH tasks, Cable supports ~/.ssh config as well as global ssh client config. Cable also supports jumping over networks, jumping over bastion hosts. Cable allows tasks composing within a host and across many different hosts.