2

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.

MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
  • without knowing what you are trying to do it is hard to answer but maybe you can run a bash script prior to the scala jar/script that will run ssh commands for you and save the result somewhere the scala code can access it? – Tal Joffe Sep 03 '17 at 08:16
  • Nononono I want to do all of it within Scala. So far using te second approach I get my result as a string. I would like to have the output stream instead. STD out. To make is simple i am trying to run ls. This works: "ssh ... ls"!! But the return in a string. I want a steam – MaatDeamon Sep 03 '17 at 08:22

2 Answers2

4

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.

MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
0

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.

Michaelzh
  • 441
  • 5
  • 14