开发者

How can I run an application on a remote machine by ssh?

How can I run an application on a remote machine via ssh?

I tried using JSCH, this way:

    Properties props = new Properties();
    props.put("StrictHostKeyChecking", "no");

    String host = "111.111.11.111";
    String user = "myuser";
    String pwd = "mypass";
    // int port = 22;

    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host);
    session.setConfig(props);
    session.setPassword(pwd);
    session.connect();

    System.out.println(session.getServerVersion());
    System.out.println("Conected!");

    // String command = "ps -ef;date;hostname";

    String command = "myapplication  some开发者_高级运维FileAsParameter";
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);

    // channel.setInputStream(System.in);
    channel.setInputStream(null);

    ((ChannelExec) channel).setErrStream(System.err);

    InputStream in = channel.getInputStream();

    channel.connect();

and I have this:

bash: myapplication: command not found

When try execute this command by shell, works fine.

Not necessarily needs to be JSCH, can be anything.


For future readers: As shown by the discussion, the reason was that JSch connected to a different server than normal SSH (and one of these had the file, while the other one had not). I still let my answer here, as it contains some useful (I think) debugging ideas for similar problems. But remember: First check that you are connecting to the right computer.


In principle, this is the way to do it, if you use JSch. If you get the error message bash: myapplication: command not found, this means that there was no myapplication binary in the path. (And you will get the same error message when using plain command line ssh.)

Try which myapplication (or type myapplication) in your normal shell, to see where it is located. Then use echo $PATH >&2 as the command, to see the path used by your exec channel. It could be that this is different then when executed from your normal shell. If so, either use the full path in the command name, or fix your setup. (Look wether the path is set in ~/.bashrc, ~/.profile, /etc/profile or /etc/bash.bashrc. Only some of these files will get executed on a interactive versus noninteractive login - and exec channel will be noninteractive, I think.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜