简体   繁体   English

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

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

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

I tried using JSCH, this way:我尝试使用 JSCH,这样:

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

and I have this:我有这个:

bash: myapplication: command not found bash:我的应用程序:找不到命令

When try execute this command by shell, works fine.当尝试通过 shell 执行此命令时,工作正常。

Not necessarily needs to be JSCH, can be anything.不一定需要是JSCH,可以是任何东西。

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).对于未来的读者:如讨论所示,原因是 JSch 连接到与普通 SSH 不同的服务器(其中一个有文件,而另一个没有)。 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.原则上,如果您使用 JSch,这就是这样做的方法。 If you get the error message bash: myapplication: command not found , this means that there was no myapplication binary in the path.如果您收到错误消息bash: myapplication: command not found ,这意味着路径中没有myapplication二进制文件。 (And you will get the same error message when using plain command line ssh .) (当使用普通命令行ssh时,您将收到相同的错误消息。)

Try which myapplication (or type myapplication ) in your normal shell, to see where it is located.在您的普通 shell 中尝试which myapplication (或type myapplication )以查看它的位置。 Then use echo $PATH >&2 as the command, to see the path used by your exec channel.然后使用echo $PATH >&2作为命令,查看 exec 通道使用的路径。 It could be that this is different then when executed from your normal shell.当从您的普通 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.) (查看路径是否设置在~/.bashrc~/.profile/etc/profile/etc/bash.bashrc中。只有其中一些文件将在交互式与非交互式登录时执行 - 并且exec通道将是我认为是非交互式的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM