2014-03-04 176 views
2

我在亚马逊机器上有一个脚本,我将使用netbeans中的ssh运行此脚本。 我使用此代码:由java运行ssh commande在ec2实例中启动脚本

String myKey="/home/local/my_key.pem"; 
Runtime runtime = Runtime.getRuntime(); 
String commande = "ssh -i "+myKey+" [email protected] 'bash runFile.bash' -o StrictHostKeyChecking=no "; 

Process p = runtime.exec(commande); 
p.waitFor(); 

但它不起作用。当我打印p.getErrorStream()我获得:

bash: writeFile.bash: command not found 

,当我从bash它的工作原理,但在NetBeans没有运行commande

有人可以解释我为什么?请问其他解决方案

感谢

+0

您能得到什么?错误消息?你可以把它添加到问题? –

+0

没有错误,只是命令行不能做它应该做的事! – Mehdi

+0

什么都不打印? –

回答

1

试试这个:

String myKey="/home/local/my_key.pem"; 
Runtime runtime = Runtime.getRuntime(); 
String commande = "ssh -i "+myKey+" [email protected] 'bash ./runFile.bash' -o StrictHostKeyChecking=no "; 

Process p = runtime.exec(commande); 
p.waitFor(); 

(使用bash ./runFile.bash而不是bash runFile.bash