通过Matlab系统连续运行多个远程命令()

时间:2014-04-30 13:55:56

标签: linux matlab ssh

我想使用matlab系统调用连续执行多个命令。我首先想要ssh到远程机器然后在该机器上运行程序。程序启动后,我想在此程序的控制台中输入另一个命令。以下是我想在代码中做的事情:

system('ssh othermachine')
system('command on other machine')
%%WAIT FOR PROGRAM TO START RUNNING ON OTHER MACHINE
system('run command on other machine')

问题是Matlab将挂起第一个系统调用,并且在退出第一个系统调用之前不会进行下一次系统调用。有没有解决的办法? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

序言:你的问题很普遍,不仅与matlab有关。

当您想通过ssh运行远程命令时,必须在ssh调用中发出它们。在(linux)shell中,你有

$ ssh remotemachine command1

单个命令。因此,使用matlab system调用

>> system('ssh remotemachine command1').

如果希望按顺序执行多个命令,则在shell中编写

$ ssh remotemachine "command1; command2"

即,在matlab中,你会写一些像

这样的东西
>> system('ssh remotemachine "command1; command2"').

一般来说,在shell脚本中对命令进行分组更为优雅,比如script.sh,并在ssh调用中将其管道化

$ cat script.sh | ssh remotemachine

,在matlab shell中,听起来像

>> system('cat script.sh | ssh remotemachine').

您可以添加许多标记以指定所需的行为(例如,在会话分离/后台执行,输出集合,...... look e.g. here方面)。

相关问题