【问题标题】:Using SSH connection for inter application communication使用 SSH 连接进行应用程序间通信
【发布时间】:2012-09-26 13:37:37
【问题描述】:

我想编写一个应用程序,它是具有两种连接类型的自定义 SSH 服务器:

  • 同步通道,客户端输入命令,服务器将返回输出
  • 用户连接并开始读取IO的流通道,服务器持续发布数据。

我在 Java 中做这件事,我认为 Apache Mina SSHD 是正确的工具。我设法编写了一些用于身份验证的代码(感谢在网上找到的资源),并且可以在我的连接上运行 /bin/sh,所以我猜我已经完成了所有设置。问题是,由于缺乏关于这些东西如何工作以及 Mina 具体如何工作的知识,我从现在开始就被困住了。

基本上我需要访问每个 SSH 连接的输入和输出流,之后我可以自己解决问题,购买什么是正确的方法来获得它?

我应该制作自定义频道吗?自定义外壳?一组自定义命令?

任何人都可以向我指出有关该主题的资源吗?

【问题讨论】:

  • 这并不能完全回答你的问题,但我会以不同的方式处理这个问题。我会将 SSH 连接与您的应用程序分开。您是否考虑过为您谈论的两个通道中的每一个打开一个端口,然后如果客户端无法直接访问这些端口,您可以让他们使用 ssh 连接中的“-L”标志通过 ssh 连接隧道这些端口命令?
  • @bohney:+1。更简单、更易于设置和维护。
  • 不确定我是否理解,但我基本上是在为我们使用的第三方服务编写一个模拟,所以我在架构选项上有点受限。我也将它集成到 Akka 应用程序中(在 scala 中)
  • 客户端实际上已经写好了,使用的是SSH库。

标签: java apache-mina sshd


【解决方案1】:

我找到了解决办法:

首先你必须实现一个命令工厂,它的完成如下:

class CommandFactory extends Factory[Command] {

  override def create():Command = {
    new Command() {
      def destroy() {}

      def setInputStream(in: InputStream) {}

      def setErrorStream(err: OutputStream) {}

      def setOutputStream(out: OutputStream) {}

      def start(env: Environment) {}

      def setExitCallback(callback: ExitCallback) {}
    }
  }
}

然后你像这样设置你的 ssh 服务器:

sshd.setShellFactory(new CommandFactory())

当然,您可以扩展实现以将所需的任何内容传递给命令。

命令的实现是您定义 shell 行为的地方。

【讨论】:

  • “当然,您可以扩展实现以将所需的任何内容传递给命令。”我如何做到这一点......我需要实现我自己的服装命令。我成功更新了我的代码,直到使用 start 和其他实现的方法创建了一个 Command 类......我的疑问是我如何捕获在 ssh 连接中输入的命令并比较它是否是我的客户命令,如果是,采取必要的行动......请急需此帮助
【解决方案2】:

这是我对问题本身发表的评论的后续行动。

要允许客户端(客户端)直接访问远程计算机(服务器)上的端口,或通过与服务器(网关)位于同一网络的另一台计算机通过 SSH 访问,您只需使用 -L 标志。

从客户端直接到服务器(客户端机器上的8080端口将隧道到服务器上的80):

ssh -L 8080:localhost:80 server

通过网关从客户端到服务器(客户端机器上的 8080 端口将通过隧道连接到服务器上的 80):

ssh -L 8080:server:80 gateway

从 ssh 的手册页中可以看出,如何使用 -L 标志:

     -L [bind_address:]port:host:hostport
         Specifies that the given port on the local (client) host is to be
         forwarded to the given host and port on the remote side.  This
         works by allocating a socket to listen to port on the local side,
         optionally bound to the specified bind_address.  Whenever a
         connection is made to this port, the connection is forwarded over
         the secure channel, and a connection is made to host port
         hostport from the remote machine.  Port forwardings can also be
         specified in the configuration file.  IPv6 addresses can be
         specified by enclosing the address in square brackets.  Only the
         superuser can forward privileged ports.  By default, the local
         port is bound in accordance with the GatewayPorts setting.
         However, an explicit bind_address may be used to bind the
         connection to a specific address.  The bind_address of
         ``localhost'' indicates that the listening port be bound for
         local use only, while an empty address or `*' indicates that the
         port should be available from all interfaces.

【讨论】:

  • 谢谢,但这并不能回答我的问题:我需要访问作为 java 缓冲区的输入和输出流,以便我可以编写自己的命令和输出流。我在服务器端的想法是读取输入,将其解释为命令,处理并将答案写入输出。对于流版本,我不会读取输入,只是在输出上发布我的数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 2014-12-09
  • 1970-01-01
  • 2010-11-26
  • 2010-10-05
  • 2015-08-18
  • 2021-06-06
相关资源
最近更新 更多