使用 java jcabi SSH 客户端(或其他)在 shell 中执行多个命令 [英] Using java jcabi SSH client (or other) to execute several commands in shell

查看:47
本文介绍了使用 java jcabi SSH 客户端(或其他)在 shell 中执行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何创建 ssh shell

I understand how to create a ssh shell

Shell ssh = new SshByPassword("192.168.1.5", 22, "admin", "password");

我也了解如何运行命令

String output = new Shell.Plain(ssh).exec("some command");

我可以轻松分析输出字符串

and i can easly analyze the output string

但是我如何在同一个shell"中一个接一个地发送命令

but how do i send in the same "shell" one command after the other

和奖励问题有时命令需要用户确认(按 Y 继续")

and bonus question sometimes the commands require a user confirmation ("press Y to continue")

图书馆可以吗?

推荐答案

通常,大多数 Java SSH API 都留给开发人员来解决在 shell 中执行多个命令的复杂性.这是一个复杂的问题,因为 SSH 不提供命令在 shell 中的开始和结束位置的任何指示.协议只提供一个数据流,这是shell的原始输出.

Generally, most Java SSH APIs leave it to the developer to sort out the complexities of executing multiple commands within a shell. It is a complicated problem because SSH does not provide any indication of where commands start and end within the shell; the protocol only provides a stream of data, which is the raw output of the shell.

我虚心介绍我的项目Maverick Synergy.一个开源 API (LGPL),它确实为交互式 shell 提供了一个接口.我在 文章.

I would humbly like to introduce my project Maverick Synergy. An open-source API (LGPL) that does provide an interface for interactive shells. I documented the options for interactive commands in an article.

这是一个非常基本的例子,ExpectShell 类允许你执行多个命令,每次返回一个封装命令输出的 ShellProcess.您可以使用 ShellProcess InputStream 来读取输出,当命令完成时它会返回 EOF.

Here is a very basic example, the ExpectShell class allows you to execute multiple commands, each time returning a ShellProcess that encapsulates the command output. You can use the ShellProcess InputStream to read the output, it will return EOF when the command is done.

您还可以使用 ShellProcessController 与命令进行交互,如本示例所示.

You can also use a ShellProcessController to interact with the command as this example shows.

SshClient ssh = new SshClient("localhost", 22, "lee", "xxxxxx".toCharArray());

ssh.runTask(new ShellTask(ssh) { 
    protected void onOpenSession(SessionChannelNG session) 
         throws IOException, SshException, ShellTimeoutException { 

         ExpectShell shell = new ExpectShell(this);

         // Execute the first command
         ShellProcess process = shell.executeCommand("ls -l");
         process.drain();
         String output = process.getCommandOutput();

         // After processing output execute another
         ShellProcessController controller =  
               new ShellProcessController(
                  shell.executeCommand("rm -i file.txt"));              

         if(controller.expect("remove")) {         
             controller.typeAndReturn("y");     
         }           

         controller.getProcess().drain();
   }
});

ssh.disconnect();

这篇关于使用 java jcabi SSH 客户端(或其他)在 shell 中执行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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