Java库可通过ssh在远程服务器上运行多个不相关的命令 [英] Java library to run multiple unrelated commands on a remote server via ssh

查看:68
本文介绍了Java库可通过ssh在远程服务器上运行多个不相关的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序必须像这样工作:

My Java application has to work like this:

  1. 用户在GUI中选择bash命令,然后按发送".
  2. 应用程序为每个命令返回不同且独立的答案(例如,我们可以将它们存储在不同的文件中).
  3. 每个命令都是交互式运行的,而不是成批运行(不能像"ls \ n pwd \ n"之类的东西)
  4. 在每个命令之后,应用程序将检查结果是否正常.如果是这样,它将发送下一条命令.
  5. 我们需要在远程主机上执行 su< user> .

这将是一个更大应用程序的插件,因此诸如尝试其他操作"(即RPC,Web服务)之类的答案不会对我有帮助:(

This will be a plugin for a bigger app, so answers like "try something else" (i.e. RPC, web services) will not help me :(

据我了解,我必须使用SHELL或至少保持频道连接.

As far as i understand i have to use SHELL or at least keep channel connected.

我已经测试了jsch,sshj和ethz.ssh2,但是结果很差.

I have tested jsch , sshj and ethz.ssh2 but with bad results.

我已经通过stackoverflow回答了诸如通过jsch-shell-channel发送命令到服务器的命令"之类的问题.但是,它们都集中于在一行中发送整个命令.我需要一个交互式的,持久的SSH会话.

I've dug throu stackoverflow answers for questions like: "sending-commands-to-server-via-jsch-shell-channel" etc. But they all focus on sending whole commands in one line. I need an interactive, persistent SSH session.

我用过ExpectJ(输出流有点乱).它解决了点1,3,4,5.

I've used ExpectJ (with a little hack of output stream). It has resolved points 1,3,4,5.

但是,第2点存在问题.在我的应用中,我需要获得单独的答案.但是我们不会知道它们的长度.命令提示可以不同.任何人都知道如何破解" ExpectJ,所以它将变得更加同步吗?我正在寻找这样的行为:发送,等待完整的答案,发送,等待... .我尝试了一些基本的同步技巧,但是这样做通常会导致超时和连接丢失.

But there is a problem with point 2. In my app I need to get separated answer. But we will not know their length. Command prompts can be different. Anyone knows how to "hack" ExpectJ so it will be some how more synchronized? I am looking for acting like this : send , wait for full answer, send, wait... I've tried some basic synchronization tricks but this end in timeouts and connection lost usually.

推荐答案

您应使用Java的 ExpectJ Unix expect 实用工具的实现.

You should use ExpectJ, a Java implementation of the Unix expect utility.

这篇关于Java库可通过ssh在远程服务器上运行多个不相关的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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