首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SSH连接进行应用程序间通信

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

Stack Overflow用户
提问于 2012-10-05 11:41:45
回答 2查看 2K关注 0票数 3

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

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

我在Java中这样做,我认为是正确的工具。我成功地编写了一些用于身份验证的代码(感谢在网络上找到的资源),并且可以在我的连接上运行/bin/sh,所以我想我已经安装好了。问题是,从现在开始,我被困住了,因为我不知道这些东西是如何工作的,米娜是如何具体地工作的。

基本上,我需要访问每个SSH连接的输入和输出流,然后我可以自己整理东西,买什么合适的方法来获得它呢?

我应该做一个定制频道吗?定制壳牌?一组自定义的命令?

有谁能告诉我有关这个问题的资料吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-09 13:23:21

我找到了解决办法:

首先,您必须实现一个命令工厂,如下所示:

代码语言:javascript
复制
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服务器:

代码语言:javascript
复制
sshd.setShellFactory(new CommandFactory())

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

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

票数 2
EN

Stack Overflow用户

发布于 2012-10-05 13:34:14

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

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

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

代码语言:javascript
复制
ssh -L 8080:localhost:80 server

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

代码语言:javascript
复制
ssh -L 8080:server:80 gateway

在ssh的手册页中,下面是如何使用-L标志:

代码语言:javascript
复制
     -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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12745539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档