Jose Fonseca
Jose Fonseca

Reputation: 416

How do I treat whitespace on SSH command sent via Scala/Java ProcessBuilder?

The Scala ProcessBuilder uses the underlying Java ProcessBuilder, which has its own whitespace treatment routine that breaks SSH commands that otherwise work on the shell. I'm trying to get the Perl backticks and system() behavior where the interpreter runs an underlying shell and executes the shell command. Is there something similar for Java/Scala?

I'm trying this, specifically:

ssh REMOTE_SERVER '/usr/bin/tail -n 25 /var/log/messages'

Where /usr/bin/tail is the right path to the command, double checked it.

To get the latest log messages from a server. It works fine on the shell, but it breaks on the ProcessBuilder with No Such file or directory. Obviously ProcessBuilder quoted or escaped the command somehow and broke it.

Thanks in advance for any ideas.

Upvotes: 0

Views: 278

Answers (1)

Jose Fonseca
Jose Fonseca

Reputation: 416

Turns out I need to put the whole remote command into the last parameter of a Seq and hope ProcessBuilder guesses the right quotes to use.

import scala.sys.process._
// .....
Seq("/usr/bin/ssh","REMOTE_HOST","/usr/bin/tail -n 25 /var/log/messages")

Very little control offered over how it treats that whole last parameter. Anyway, this works. If anyone knows of a library that works more like the Perl backtick or system() behavior I'd really appreciate it (no problem if it's not 100% portable, this will only run under Linux). Thanks.

Upvotes: 0

Related Questions