Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SSH连接进行应用程序间通信_Java_Apache Mina_Sshd - Fatal编程技术网

Java 使用SSH连接进行应用程序间通信

Java 使用SSH连接进行应用程序间通信,java,apache-mina,sshd,Java,Apache Mina,Sshd,我想编写一个应用程序,它将是一个具有两种连接类型的自定义SSH服务器: 同步通道,客户端在其中键入命令,服务器将返回输出 用户连接并开始读取IO的流通道,服务器连续发布数据 我正在用Java做这件事,我认为ApacheMina SSHD是正确的工具。我设法编写了一些用于身份验证的代码(感谢网络上的资源),并且可以在我的连接上运行/bin/sh,所以我想我已经做好了设置。问题是,从现在起,我陷入了困境,因为我不知道这些东西是如何工作的,以及Mina是如何具体工作的 基本上,我需要访问每个SSH

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

  • 同步通道,客户端在其中键入命令,服务器将返回输出
  • 用户连接并开始读取IO的流通道,服务器连续发布数据
我正在用Java做这件事,我认为ApacheMina SSHD是正确的工具。我设法编写了一些用于身份验证的代码(感谢网络上的资源),并且可以在我的连接上运行/bin/sh,所以我想我已经做好了设置。问题是,从现在起,我陷入了困境,因为我不知道这些东西是如何工作的,以及Mina是如何具体工作的

基本上,我需要访问每个SSH连接的输入和输出流,然后我可以自己解决问题,买什么样的正确方法

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


有人能给我指出关于这个问题的参考资料吗?

这是我对问题本身的评论的后续

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

直接从客户端到服务器(客户端计算机上的端口8080将通过隧道传输到服务器上的端口80):

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

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

我找到了解决办法:

首先,您必须实现一个命令工厂,具体操作如下:

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行为的地方。

这并不能完全回答您的问题,但我会以不同的方式来处理这个问题。我会将SSH连接与应用程序分离。您是否考虑过为您提到的两个通道中的每一个打开一个端口,然后如果客户端无法直接访问这些端口,您可以让他们使用ssh connection命令中的'-L'标志通过ssh连接隧道这些端口?@bohney:+1。更简单,更容易安装和维护。我不确定我是否理解,但我基本上是在为我们使用的第三方服务编写一个模拟,所以我对架构选项有点限制。我还在Akka应用程序(在scala中)中集成此功能。客户端实际上已经使用SSH库编写好了。谢谢,但这并不能回答我的问题:我需要访问作为java缓冲区的输入和输出流,以便我可以编写自己的命令和输出流。我在服务器端的想法是读取输入,将其解释为命令,处理并在输出上写入答案。对于流版本,我不会读取输入,只会在输出上发布数据“当然,您可以扩展实现以将所需的任何内容传递给命令”。我如何实现这一点。。。。我需要执行我自己的服装命令。我成功地更新了代码,直到用start和其他实现的方法创建了一个命令类。。。。我的疑问是如何捕获ssh连接中输入的命令,并比较它是否是我的客户命令,如果是,则执行必要的操作。。。请紧急需要这个帮助
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) {}
    }
  }
}
sshd.setShellFactory(new CommandFactory())