使用Scala通过ssh远程发出命令时出现问题 [英] Problems using scala to remotely issue commands via ssh

查看:62
本文介绍了使用Scala通过ssh远程发出命令时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想通过ssh远程创建目录时,scala出现问题.

I have a problem with scala when I want to create a directory remotely via ssh.

ssh命令(例如date或ls)可以正常工作.

ssh commands via scala, such as date or ls, work fine.

但是,当我运行例如

"ssh user@Main.local 'mkdir Desktop/test'".!

我得到: bash:mkdir桌面/测试:没有这样的文件或目录res7:Int = 127

当我将命令复制并粘贴到外壳程序中时,它会执行而没有任何问题.

When I copy-paste the command into my shell it executes without any problems.

有人知道发生了什么吗?

Does anybody know what is going on??

我发现了这篇文章:但是,我唯一可以摆脱的是使用要创建目录的完整路径.但是,它仍然不起作用:(

However, the only thing I could take away from it is to use the full path for the directory to be created. However, it still does not work :(

谢谢!

推荐答案

ssh 不需要您传递要作为单个参数运行的整个命令行.您可以向它传递多个参数,一个用于要运行的命令,一个用于要传递该命令的任何参数.

ssh doesn't require that you pass the entire command line you want to run as a single argument. You're allowed to pass it multiple arguments, one for the command you want to run, and more for any arguments you want to pass that command.

因此,这应该可以正常工作,不用单引号:

So, this should work just fine, without the single quotes:

"ssh user@Main.local mkdir Desktop/test"

这显示了如何在普通的bash shell中获得相同的错误消息,而不涉及ssh或Scala:

This shows how to get the same error message in an ordinary bash shell, without involving ssh or Scala:

bash-3.2$ ls -d Desktop
Desktop
bash-3.2$ 'mkdir Desktop/test'
bash: mkdir Desktop/test: No such file or directory
bash-3.2$ mkdir Desktop/test
bash-3.2$ 

为了娱乐,还请注意:

bash-3.2$ mkdir 'mkdir Desktop'
bash-3.2$ echo echo foo > 'mkdir Desktop'/test
bash-3.2$ chmod +x 'mkdir Desktop'/test
bash-3.2$ 'mkdir Desktop/test'
foo

更新:

请注意,这两种方法也都可以工作:

Note that both of these work too:

Process(Seq("ssh", "user@Main.local", "mkdir Desktop/test")).!
Process(Seq("ssh", "user@Main.local", "mkdir", "Desktop/test")).!

使用采用 Seq Process.apply 的形式,消除了关于参数之间的边界位于何处的模糊性.但是请注意,命令到达远程主机后,将由远程外壳程序处理,外壳程序将自行决定将参数中断的位置.因此,例如,如果您要创建一个名称中带有空格的目录,则可以在本地运行:

Using the form of Process.apply that takes a Seq removes one level of ambiguity about where the boundaries between the arguments lie. But note that once the command reaches the remote host, it will be processed by the remote shell which will make its own decision about where to put the argument breaks. So for example if you wanted to make a directory with a space in the name, this works locally:

Process(Seq("mkdir", "foo bar")).!

但是,如果您远程尝试相同的操作:

but if you try the same thing remotely:

Process(Seq("ssh", "user@Main.local", "mkdir", "foo bar")).!

由于远程外壳程序将插入参数break,因此您将获得两个名为 foo bar 的目录.

You'll get two directories named foo and bar, since the remote shell inserts an argument break.

这篇关于使用Scala通过ssh远程发出命令时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆