scalaammonite

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


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 user@Main.local 'mkdir Desktop/test'".!

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


Solution

  • 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.