开发者

how to use java runtime.exec() and ssh to another machine to start a process

开发者 https://www.devze.com 2023-03-11 15:20 出处:网络
is this the right approach? public void doSomething() { Process p; String[] cmd = {\"/usr/bin/ssh\", \"someRemoteMachine\", \"/absPathToMyProg/myProg\"};

is this the right approach?

 public void doSomething()
 {
    Process p;

    String[] cmd = {"/usr/bin/ssh", "someRemoteMachine", "/absPathToMyProg/myProg"};
    String[] envp = {"PATH=path_needed_toRun_myProg"};

    try
    {
        p = Runtime.getRuntime().exec(cmd,e开发者_如何学Gonvp);

    }
    catch (IOException e)
    {
        System.err.println("Epic Fail");
    }
 }


Have you tried JSch. Its released under BSD type license. Its pure java and easy to use.


Apart from using JSch (or any other Java SSH implementation), like Suraj said, passing the Path via environment variables is likely not to work, since most SSH deamons only accept a small set of variables from the other side (mostly related to localization or terminal type).

As the argument to ssh (or the "command", if using JSch with an ChannelExec) is passed to the remote shell for execution, you could try to define the path in this command (if your default shell is something compatible to the POSIX sh):

PATH=path_needed_toRun_myProg /absPathToMyProg/myProg

Your array for Runtime.exex thus would look like this:

String[] cmd = {"/usr/bin/ssh", "someRemoteMachine",
                "PATH=path_needed_toRun_myProg /absPathToMyProg/myProg"};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号