如何通过ssh在远程计算机上运行应用程序? [英] How can I run an application on a remote machine by ssh?

查看:931
本文介绍了如何通过ssh在远程计算机上运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ssh在远程计算机上运行应用程序?

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

我尝试使用JSCH,这样:

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  someFileAsParameter";
    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();

我有这个:

bash:myapplication:command not found

当尝试通过shell执行此命令时,工作正常。

When try execute this command by shell, works fine.

不一定需要是JSCH,可以是任何东西。

Not necessarily needs to be JSCH, can be anything.

推荐答案

对于未来的读者 :如讨论所示,原因是JSch连接到不同于普通SSH的服务器(其中一个有文件,而另一个没有)。我仍然在这里给出答案,因为它包含一些有用的(我认为)针对类似问题的调试思路。但请记住:首先检查您是否连接到正确的计算机

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.

原则上,如果你使用JSch,这就是这样做的方法。如果您收到错误消息 bash:myapplication:command not found ,这意味着没有 myapplication 二进制文件路径。 (当使用普通命令行 ssh 时,您将收到相同的错误消息。)

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.)

尝试你的普通shell中的myapplication (或输入myapplication ),以查看它的位置。然后使用 echo $ PATH>& 2 作为命令,查看exec频道使用的路径。可能是这与从普通shell执行时不同。如果是这样,请使用命令名称中的完整路径,或修复您的设置。 (看看路径设置在〜/ .bashrc 〜/ .profile / etc / profile /etc/bash.bashrc 。只有部分文件将在交互式非交互式登录中执行 - 并且 exec 频道将是非交互式的,我认为。)

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.)

这篇关于如何通过ssh在远程计算机上运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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